usingnamespacestd; using LL = longlong; using pii = pair<int, int>;
constint N = 1 << 18; constint maxN = 2e5 + 5;
int n; LL ans; int a[maxN]; int fa[maxN], size[maxN]; pii large[maxN]; pii best[N + 5][2];
intFind(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
inlineboolMerge(int u, int v) { int fu = Find(u), fv = Find(v); if (fu == fv) returnfalse; if (size[fu] > size[fv]) swap(fu, fv); fa[fu] = fv, size[fv] += size[fu]; returntrue; }
inlinevoidTrans(pii A[2], pii B[2]) { for (int i = 0; i < 2; ++i) { pii t = B[i]; if (t.fir > A[0].fir or (t.fir == A[0].fir and t.sec != A[0].sec)) { if (A[0].sec != t.sec) A[1] = A[0]; A[0] = t; } elseif (t.fir > A[1].fir and t.sec != A[0].sec) A[1] = t; } }
intmain() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n + 1; ++i) fa[i] = i, size[i] = 1; for (int i = 1; i <= n; ++i) cin >> a[i], ans -= a[i];
int cnt = n + 1; while (cnt > 1) { for (int i = 0; i < N; ++i) best[i][0] = best[i][1] = pii(-1, -1); for (int i = 1; i <= n + 1; ++i) { int id = Find(i); if (best[a[i]][0].sec == -1) best[a[i]][0] = pii(a[i], id); elseif (best[a[i]][1].sec == -1and best[a[i]][0].sec != id) best[a[i]][1] = pii(a[i], id); } for (int i = 0; i < 18; ++i) for (int S = 0; S < N; ++S) if (S >> i & 1) { int T = S ^ 1 << i; Trans(best[S], best[T]); } for (int i = 1; i <= n + 1; ++i) large[i] = pii(-1, -1); for (int i = 1; i <= n + 1; ++i) { int S = (N - 1) ^ a[i], id = Find(i); if (best[S][0].fir == -1) continue; if ((best[S][0].fir ^ a[i]) > large[id].fir and best[S][0].sec != id) large[id] = best[S][0], large[id].fir ^= a[i]; if ((best[S][1].fir ^ a[i]) > large[id].fir and best[S][1].sec != id) large[id] = best[S][1], large[id].fir ^= a[i]; } for (int i = 1; i <= n + 1; ++i) if (Find(i) == i and large[i].fir != -1and Merge(large[i].sec, i)) --cnt, ans += large[i].fir; } cout << ans << endl; return0; }