fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int N = 1e6 + 5;
  12.  
  13. int n;
  14. int a[N];
  15.  
  16. int sos[1 << 21][2];
  17.  
  18. void add(int mx[2], int x) {
  19. if (x > mx[0]) {
  20. mx[1] = mx[0];
  21. mx[0] = x;
  22. }
  23. else {
  24. mx[1] = max(mx[1], x);
  25. }
  26. }
  27.  
  28. bool check(int ans) {
  29. for (int i = 1; i <= n; i++) {
  30. int x = (a[i] ^ ans) & ans;
  31. if (sos[x][1] > i) return true;
  32. }
  33. return false;
  34. }
  35.  
  36. int main() {
  37. ios::sync_with_stdio(false);
  38. cin.tie(nullptr);
  39. cin >> n;
  40. for (int i = 1; i <= n; i++) cin >> a[i];
  41.  
  42. for (int i = 1; i <= n; i++) add(sos[a[i]], i);
  43.  
  44. for (int i = 0; i < 21; i++) {
  45. for (int mask = 0; mask < (1 << 21); mask++) {
  46. if (!(mask & (1 << i))) {
  47. add(sos[mask], sos[mask ^ (1 << i)][0]);
  48. add(sos[mask], sos[mask ^ (1 << i)][1]);
  49. }
  50. }
  51. }
  52.  
  53. int ans = 0;
  54. for (int i = 20; i >= 0; i--) {
  55. if (check(ans | (1 << i))) ans |= (1 << i);
  56. }
  57.  
  58. cout << ans << '\n';
  59. }
Success #stdin #stdout 0.11s 21540KB
stdin
3
2 4 6
stdout
6