#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 22) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 22];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (org.first == des.first || org.first == des.second) {
        org.first = 0;
    }
    if (org.second == des.first || org.second == des.second) {
        org.second = 0;
        swap(org.first, org.second);
    }
    if (des.second <= org.second) {
        swap(des.second, des.first);
        des.second = org.second;
        swap(org.first, org.second);
    }
    if (des.first < org.second) {
        des.first = org.second;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 22; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
        assert(f[i].first <= f[i].second);
    }
    int ans = 0;
    for (int i = 1; i <= N - 2; ++i) {
        int s = 0;
        for (int j = 22; j; --j) {
            if ((getbit(j) & s) == 0 && f[s | getbit(j)].first >= i) s |= getbit(j);
        }
        ans = max(ans, (A[i] | s));
    }
    cout << ans << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
#define N 2097153
int n,a[N],f[N],ans;
void Ins(int x,int k)
{
	if(f[x]>=2)return;if(k==-1){++f[x];return;}
	Ins(x,k-1);if((x>>k)&1)Ins(x^(1<<k),k-1);
}
int main(){
	scanf("%d",&n);int i,j,x,res,t;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		res=t=0;x=a[i];
		if(i<=(n-2))
		{
			for(j=20;j>=0;j--)
			if((x>>j)&1)res|=(1<<j);
			else if(f[t|(1<<j)]>=2){res|=(1<<j);t|=(1<<j);}
			ans=max(ans,res);Ins(x,20);
		}
	}printf("%d",ans);return 0;
}


#include<bits/stdc++.h>
#define LG 22
#define N (1<<LG)
#define P 1000000007
#define pb push_back
#define mp make_pair
#define re(x, n) for (int x = 0; x < (n); ++x)
#define rep(x, l, r) for (int x = (l); x <= (r); ++x)
#define repd(x, r, l) for (int x = (r); x >= (l); --x)

int getint() {
	int x = 0, s = 1; char d = getchar();
	while (d < '0' || d > '9') { if (d == '-') s = -1; d = getchar(); }
	while (d >= '0' && d <= '9') x = x * 10 + d - '0', d = getchar();
	return x * s;
}

using namespace std;
typedef pair<int, int> pii;

int n, a[N];
pii f[N];

void udt(int x, int v) {
    if (!v) return;
    pii &pi = f[x];
    if (v > pi.first) {
        pi.second = pi.first;
        pi.first = v;
    } else if (v > pi.second) {
        pi.second = v;
    }
}

int main() {
    n = getint();
    re(i, n) a[i] = getint(), udt(a[i], i + 1);
    repd(i, N - 1, 0) {
        if (!f[i].first) continue;
        for (int j = 0; j < LG; ++j) {
            if (!(i&(1<<j))) continue;
            udt(i^(1<<j), f[i].first);
            udt(i^(1<<j), f[i].second);
        }
    }
    int ans = 0;
    re(i, n) {
        int mx = 0;
        for (int j = LG - 1; ~j; --j) {
            if (a[i]&(1<<j)) continue;
            if (f[mx|(1<<j)].second > i) mx |= 1<<j;
        }
        ans = max(ans, a[i] | mx);
    }
    cout << ans << endl;
}


// luogu-judger-enable-o2
#include <cstdio>
#include <set>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <list>
#define fo(a, b, c) for (register int a = b; a <= c; ++a)

namespace IO
{
    inline char gc()
    {
        static char s[1<<20|1]={0},*p1=s,*p2=s;
        return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
    }
//	inline char gc() { return getchar(); }
	inline long long read()
	{
		long long ret=0;bool flag=0;char c=gc();
		while ((c<'0')|(c>'9')) flag ^= !(c^'-'),c=gc();
		while ((c>='0')&(c<='9')) ret=(ret<<1)+(ret<<3)+(c^'0'),c=gc();
		return flag?-ret:ret;
 	}
 	char OutputAns[1<<20|1],*OutputCur = OutputAns;
 	inline void output()
 	{
 		OutputCur -= fwrite(OutputAns,1,OutputCur - OutputAns,stdout);
	}
	inline void print(long long ans)
	{
		char s[20]={0};
		if (OutputCur - OutputAns + sprintf(s,"I64d",ans) >> 20) output();
		OutputCur += sprintf(OutputCur,"%I64d",ans);
	}
	inline void printc(char c)
	{
		if (OutputCur - OutputAns + 1 >> 20) output();
		*(OutputCur++) = c;
	}
}

using IO::read;
using IO::print;
using IO::printc;
using IO::output;

typedef long long int64;

const int M = 1e6 + 11;

int n, ans;

int A[M], Cur[(1<<21)], Vis[(1<<21)];

inline void Ins(int num,int tim)
{
	if (Cur[num] == 2 || Vis[num] == tim) return ;
	++Cur[num]; Vis[num] = tim;
	for (int i = 20;~i; --i)
		if ((num >> i) & 1)
			Ins(num ^ (1<<i), tim);
}

int main(void)
{
	n = read();
	for (int i = 1;i <= n; ++i)
		A[i] = read();
	for (int i = n;i; --i)
	{
		int s = 0;
		for (int j = 20;j >= 0;--j)
			if (!((A[i]>>j) & 1))
				if (Cur[s | (1<<j)] == 2)
					s |= (1<<j);
		if (i <= n - 2) ans = std::max(ans, A[i] | s);
		Ins(A[i], i);
	} printf("%d\n", ans);
}


#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char nc() {
//	return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x) {
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
const int maxn = 1e6 + 5;
int n;
int a[maxn];
int bit[25];
struct data {
	int x, y;
	data(int x=-1,int y=-1):x(x),y(y){}
	void update(int d) {
		if(d == x || d == y) return;
		if(d > x) {
			y = x, x = d;
		}
		else if(d > y) {
			y = d;
		}
	}
} f[1 << 21];
void init(int n) {
	bit[0] = 1;
	for(int i = 1; i <= n; ++i) bit[i] = bit[i - 1] << 1;
}
int main() {
	init(21);
	read(n);
	for(int i = 1; i <= n; ++i) {
		read(a[i]);
		f[a[i]].update(i);
	}
	for(int i = 0; i < 21; ++i) {
		for(int s = 0; s < bit[21]; ++s) {
			if(s & bit[i]) {
				f[s ^ bit[i]].update(f[s].x);
				f[s ^ bit[i]].update(f[s].y);
			}
		}
	}
	int an = 0;
	for(int i = 1; i <= n - 2; ++i) {
		int u = 0;
		for(int k = 20; ~k; --k) {
			if(a[i] & bit[k]) continue;
			if(f[u | bit[k]].y > i) u |= bit[k];
		}
		an = max(an, a[i] | u);
	}
	printf("%d\n", an);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int a[maxn];
int read(){
    int x=0,y=1;
    char ch=getchar();
    while(ch<48||ch>57){if(ch==45)y=-1;ch=getchar();}
    while(ch>=48&&ch<=57)x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*y;
}
int mx[(1<<21)],cx[(1<<21)];
inline void update(int k,int x){
	if(mx[k]<x){cx[k]=mx[k];mx[k]=x;}
	else if(mx[k]==x)cx[k]=x;
	else cx[k]=max(cx[k],x);
	return ;
}
void sos(int len){
	for(int i=0;i<21;i++)
		for(int j=0;j<len;j++)
			if(!(j&(1<<i))){
				update(j,mx[j^(1<<i)]);
				update(j,cx[j^(1<<i)]);
			}
	return ;
}
int main(){
   	int n;
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
		update(a[i],i);
	}
	sos(1<<21);
	int S=(1<<21)-1,ans=0;
	for(int i=1;i<=n-2;i++){
		int c=S^a[i];
		int s=0;
		for(int j=21;j>=0;j--){
			if(!((c>>j)&1))continue;
			s+=(1<<j);
			if(cx[s]<=i)s-=(1<<j);
		}
		ans=max(ans,a[i]+s);
	}
	printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
#define N 1048577
int n,a[N],f[N],ans;
void Ins(int x,int k)
{
	if(f[x]>=2)return;if(k==-1){++f[x];return;}
	Ins(x,k-1);if((x>>k)&1)Ins(x^(1<<k),k-1);
}
int main(){
	scanf("%d",&n);int i,j,x,res,t;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		res=t=0;x=a[i];
		for(j=19;j>=0;j--)
		if((x>>j)&1)res|=(1<<j);
		else if(f[t|(1<<j)]>=2){res|=(1<<j);t|=(1<<j);}
		ans=max(ans,res);Ins(x,19);
	}printf("%d",ans);return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 21) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 21];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (des.second < org.second) {
        swap(des.second, des.first);
        des.second = org.second;
        swap(org.first, org.second);
    }
    if (des.first < org.second) {
        des.first = org.second;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 21; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
        assert(f[i].first <= f[i].second);
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 21; j; --j) {
            if (!(getbit(j) & s) && f[s | getbit(j)].first > i) s |= getbit(j);
        }
        ans = max(ans, (A[i] | s));
    }
    cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e6+9,M=(1<<21);
int n,a[N],mx[N],Mx[N],ans;

inline void insert(int x,int c)
{
	if(c>mx[x]) Mx[x]=mx[x],mx[x]=c;
	else Mx[x]=max(Mx[x],c);
}

inline void FMT()
{
	for(int i=0;i<21;i++)
		for(int S=M-1;S>=0;S--)
			if(!(S>>i&1))
			{
				insert(S,mx[S|(1<<i)]);
				insert(S,Mx[S|(1<<i)]);
			}
}

int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]),insert(a[i],i);
	FMT();
	int all=M-1;
	for(int i=1;i<=n-2;i++)
	{
		int S=all^a[i],res=0;
		for(int j=20;j>=0;j--)
			if(S>>j&1)
			{
				if(Mx[res|(1<<j)]>i) res|=(1<<j);
			}
		ans=max(ans,res+a[i]);
	}
	printf("%d",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN=1000000;
const int MAXS=2000000;
const int MAXW=20;
int N,A[MAXN+5],f[MAXS+5][2],ans;

void update(int x,int pos);
void dp();
void Input();
int main()
{
	Input();
	dp();
	printf("%d\n",ans);
	return 0;
}
void update(int S,int pos)
{
	if(f[S][0]<pos && pos!=f[S][1]) f[S][0]=pos;
	if(f[S][0]>f[S][1]) swap(f[S][0],f[S][1]);
	return;
}
void dp()
{
	ans=0;
	for(int i=1;i<=N;i++)//i<j<k, 所以从i开始向后
		f[A[i]][1]=i;//A[i]是A[i]的子集(边界)
	for(int S=MAXS;S>=0;S--)//倒序枚举，这样可以先更新S再更新S的子集
		for(int i=MAXW;i>=0;i--)
			if(S&(1<<i))//枚举S的元素
			{//用S的可选集合 更新与S仅差一个元素的子集的可选集合
				update(S^(1<<i),f[S][1]);
				update(S^(1<<i),f[S][0]);
			}
	for(int i=1;i<=N-2;i++)
	{
		int w=0;
		for(int j=MAXW;j>=0;j--)//找到Cu(A[i])的元素
			if((A[i]&(1<<j))==0 && f[w|(1<<j)][1]>i && f[w|(1<<j)][0]>i)//贪心地从Cu(A[i])的最高位选起
				w=w|(1<<j);
		ans=max(ans,A[i]|w);
	}
	return;
}
void Input()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
		scanf("%d",&A[i]);
	return;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001, fullVal = (1 << 21) - 1;
typedef pair<int, int> Pair;
Pair f[1 << 21];
int A[maxn];
#define getbit(x) (1 << ((x) - 1))
inline void add(Pair& des, Pair org) {
    if (des.second < org.second) {
        swap(des.second, des.first);
        des.second = org.second;
        swap(org.first, org.second);
    }
    if (des.first < org.second) {
        des.first = org.second;
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i) {
	    cin >> A[i];
	    add(f[A[i]], make_pair(0, i));
    }
    for (int i = fullVal; i; --i) {
        for (int j = 1; j <= 21; ++j) {
            if (getbit(j) & i) add(f[i ^ getbit(j)], f[i]);
        }
    }
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 21; j; --j) {
            if (!(getbit(j) & s) && f[s | getbit(j)].first > i) s |= getbit(j);
        }
        ans = max(ans, (A[i] | s));
    }
    cout << ans << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
#define N 1048577
int n,a[N],f[20][N],ans;
void Ins(int x,int k)
{
	if(f[k][x]>=2)return;++f[k][x];if(k>=19)return;
	Ins(x,k+1);if((x>>k)&1)Ins(x^(1<<k),k+1);
}
int main(){
	scanf("%d",&n);int i,j,x,res,t;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		res=t=0;x=a[i];
		for(j=19;j>=0;j--)
		if((x>>j)&1)res|=(1<<j);
		else if(f[j][t|(1<<j)]>=2){res|=(1<<j);t|=(1<<j);}
		ans=max(ans,res);Ins(x,0);
	}printf("%d",ans);return 0;
}


#include<bits/stdc++.h>
#define re register
using namespace std;
const int maxn=1e6+5;
const int maxm=(1<<21)+5;
int a[maxn],n,wei,tmp[3];
pair<int,int>dp[maxm];

void Max(int &a,int b) {
	if(a<b)a=b;
}

pair<int,int> operator+(const pair<int,int> &a,const int &b) {
	if(!b)return a;
	tmp[0]=a.first,tmp[1]=a.second,tmp[2]=b;
	sort(tmp,tmp+3);
	return make_pair(tmp[1],tmp[2]);
}

pair<int,int> operator+(const pair<int,int> &a,const pair<int,int> &b) {
	pair<int,int>tmp=a+b.first+b.second;
	return tmp;
}

bool check(pair<int,int> x,int pr) {
	return x.second&&x.first>pr;
}

int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)scanf("%d",&a[i]),dp[a[i]]=dp[a[i]]+i,Max(wei,(int)log2(a[i]));
	int w=(1<<wei+1)-1;
//	printf("wei : %d,w : %d\n",wei,w);
	for(int i=0; i<=wei; i++)
		for(re int j=0; j<=w; j++)
			if(j&(1<<i))dp[j^(1<<i)]=dp[j^(1<<i)]+dp[j];
	int ans=0;
	for(int i=1; i<=n; i++) {
		int step=w-a[i],S=0;
		for(int j=wei; j>=0; j--)if(step&(1<<j)&&check(dp[S|(1<<j)],i))S|=(1<<j);
		if(check(dp[S],i))Max(ans,S|a[i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

int n;
int cnt[N], a[N];
void insert(int x, int h)
{
    if (cnt[x] >= 2) return;
    if (h < 0) {
        cnt[x] ++;
        return;
    }
    insert(x, h - 1);
    if (x>>h & 1) insert(x^(1<<h), h - 1);
}
int query(int x)
{
    int ans = 0;
    for (int i = 20; i >= 0; i -- )
    {
        if ((ans>>i&1)==0 && cnt[ans|1<<i] >= 2) ans |= (1<<i);
    }
    return x|ans;
}
int main ()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
        scanf("%d", &a[i]);
    int ans = 0;
    for (int i = n; i >= 1; i -- )
    {
        if (n - i >= 2)
        {
            ans = max(ans, query(a[i]));
        }
        insert(a[i], 20);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

int n;
int cnt[N], a[N];
void insert(int x, int h)
{
    if (cnt[x] >= 2) return;
    if (h < 0) {
        cnt[x] ++;
        return;
    }
    insert(x, h - 1);
    if (x>>h & 1) insert(x^(1<<h), h - 1);
}
int query(int x)
{
    int ans = 0;
    for (int i = 21; i >= 0; i -- )
    {
        if ((ans>>i&1)==0 && cnt[ans|1<<i] >= 2) ans |= (1<<i);
    }
    return x|ans;
}
int main ()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
        scanf("%d", &a[i]);
    int ans = 0;
    for (int i = n; i >= 1; i -- )
    {
        if (n - i >= 2)
        {
            ans = max(ans, query(a[i]));
        }
        insert(a[i], 21);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 3000;

int n;
int cnt[N], a[N];
void insert(int x, int h)
{
    if (cnt[x] >= 2) return;
    if (h < 0) {
        cnt[x] ++;
        return;
    }
    insert(x, h - 1);
    if (x>>h & 1) insert(x^(1<<h), h - 1);
}
int query(int x)
{
    int ans = 0;
    for (int i = 21; i >= 0; i -- )
    {
        if ((ans>>i&1)==0 && cnt[ans|1<<i] >= 2) ans |= (1<<i);
    }
    return x|ans;
}
int main ()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
        scanf("%d", &a[i]);
    int ans = 0;
    for (int i = n; i >= 1; i -- )
    {
        if (n - i >= 2)
        {
            ans = max(ans, query(a[i]));
        }
        insert(a[i], 21);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

typedef pair<int,int> PII;
#define x first
#define y second
PII f[N];
int n;
int a[N];
void update(int c, int pos)
{
    if (pos > f[c].x)
    {
        f[c].y = f[c].x;
        f[c].x = pos;
    } else if (pos > f[c].y)
        f[c].y = pos;
}
void merge(PII &a, PII b)
{
    if (a.x < b.x) swap(a, b);
    if (a.y < b.x) a.y = b.x;
}
void get_dp()
{
    for (int j = 0; j <= 20; j ++ )
    {
        for (int i = N - 1; i >= 0; i -- )
        {
            if(i>>j & 1)
            {
                merge(f[i^(1<<j)], f[i]);
            }
        }
    }
}
int main ()
{
    int n;
    scanf("%d", &n);
    memset(f, 0, sizeof f);
    for (int i = 1; i <= n; i ++ ) {
        scanf("%d", &a[i]);
        update(a[i], i);
    }

    get_dp();

    int ans = 0;
    for (int i = 1; i <= n; i ++ )
    {
        int temp = 0;
        for (int j = 20; j >= 0; j -- )
        {
            int check = temp ^ (1<<j);
            if ((a[i] >> j & 1) == 0 && f[check].y > i)
                temp = check;
        }
        ans = max(temp | a[i], ans);
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

typedef pair<int,int> PII;
#define x first
#define y second
PII f[N];
int n;
int a[N];
void update(int c, int pos)
{
    if (pos > f[c].x)
    {
        f[c].y = f[c].x;
        f[c].x = pos;
    } else if (pos > f[c].y)
        f[c].y = pos;
}
void merge(PII &a, PII b)
{
    if (a.x < b.x) swap(a, b);
    if (a.y < b.x) a.y = b.x;
}
void get_dp()
{
    for (int j = 0; j <= 20; j ++ )
    {
        for (int i = N - 1; i >= 0; i -- )
        {
            if(i>>j & 1)
            {
                merge(f[i^(1<<j)], f[i]);
            }
        }
    }
}
int main ()
{
    int n;
    scanf("%d", &n);
    memset(f, 0, sizeof f);
    for (int i = 1; i <= n; i ++ ) {
        scanf("%d", &a[i]);
        update(a[i], i);
    }

    get_dp();

    int ans = 0;
    for (int i = 1; i <= n - 2; i ++ )
    {
        int temp = 0;
        for (int j = 20; j >= 0; j -- )
        {
            int check = temp ^ (1<<j);
            if ((a[i] >> j & 1) == 0 && f[check].y > i)
                temp = check;
        }
        ans = max(temp | a[i], ans);
    }

    printf("%d\n", ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 3000;

int n;
int cnt[N*2], a[N];
void insert(int x, int h)
{
    if (cnt[x] >= 2) return;
    if (h < 0) {
        cnt[x] ++;
        return;
    }
    insert(x, h - 1);
    if (x>>h & 1) insert(x^(1<<h), h - 1);
}
int query(int x)
{
    int ans = 0;
    for (int i = 21; i >= 0; i -- )
    {
        if ((ans>>i&1)==0 && cnt[ans|1<<i] >= 2) ans |= (1<<i);
    }
    return x|ans;
}
int main ()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
        scanf("%d", &a[i]);
    int ans = 0;
    for (int i = n; i >= 1; i -- )
    {
        if (n - i >= 2)
        {
            ans = max(ans, query(a[i]));
        }
        insert(a[i], 21);
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char nc() {
	return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x) {
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
const int maxn = 1e6 + 5;
const int maxnode = maxn * 16;
int n;
int a[maxn];
namespace tri {
	int all;
	int root;
	int siz[maxnode];
	int val[maxnode];
	int ch[maxnode][2];
	vector<int> temp;
	inline void newnode(int &u) {
		if(temp.size()) u = temp.back(), temp.pop_back();
		else u = ++all;
		siz[u] = 0;
		val[u] = 0;
		ch[u][0] = ch[u][1] = 0;
	}
	void remove(int u) {
		if(u == 0) return;
		temp.push_back(u);
		remove(ch[u][0]);
		remove(ch[u][1]);
	}
	void insert(int &u, int x, int k) {
		if(u == 0) newnode(u);
		++siz[u];
		if(k == -1) {
			return;
		}
		int c = bool(x & (1 << k));
		if(c == 1) insert(ch[u][c], x, k - 1);
		else {
			if(~val[u] & (1 << k)) insert(ch[u][c], x, k - 1);
		}
		if(siz[ch[u][1]] >= 2) {
			val[u] = (1 << k) | val[ch[u][1]];
			remove(ch[u][0]);
		}
		else {
			val[u] = max(val[ch[u][1]], val[ch[u][0]]);
		}
	}
}
int main() {
	read(n);
	for(int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	tri :: insert(tri :: root, a[n], 20);
	int an = 0;
	for(int i = n - 1; i > 1; --i) {
		tri :: insert(tri :: root, a[i], 20);
		an = max(an, a[i - 1] | tri :: val[tri :: root]);
	}
	printf("%d\n", an);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char nc() {
	return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x) {
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
const int maxn = 1e6 + 5;
const int maxnode = maxn * 16;
int n;
int a[maxn];
namespace tri {
	int all;
	int root;
	int siz[maxnode];
	short val[maxnode];
	int ch[maxnode][2];
	vector<int> temp;
	inline void newnode(int &u) {
		if(temp.size()) u = temp.back(), temp.pop_back();
		else u = ++all;
		siz[u] = 0;
		val[u] = 0;
		ch[u][0] = ch[u][1] = 0;
	}
	void remove(int u) {
		if(u == 0) return;
		temp.push_back(u);
		remove(ch[u][0]);
		remove(ch[u][1]);
	}
	void insert(int &u, int x, int k) {
		if(u == 0) newnode(u);
		++siz[u];
		if(k == -1) {
			return;
		}
		int c = bool(x & (1 << k));
		if(c == 1) insert(ch[u][c], x, k - 1);
		else {
			if(~val[u] & (1 << k)) insert(ch[u][c], x, k - 1);
		}
		if(siz[ch[u][1]] >= 2) {
			val[u] = (1 << k) | val[ch[u][1]];
			remove(ch[u][0]);
		}
		else {
			val[u] = max(val[ch[u][1]], val[ch[u][0]]);
		}
	}
}
int main() {
	read(n);
	for(int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	tri :: insert(tri :: root, a[n], 20);
	int an = 0;
	for(int i = n - 1; i > 1; --i) {
		tri :: insert(tri :: root, a[i], 20);
		an = max(an, a[i - 1] | tri :: val[tri :: root]);
	}
	printf("%d\n", an);
	return 0;
}


#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char nc() {
//	return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x) {
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
const int maxn = 1e6 + 5;
int n;
int a[maxn];
int bit[25];
struct data {
	int x, y;
	data(int x=-1,int y=-1):x(x),y(y){}
	void update(int d) {
		if(d == x || d == y) return;
		if(d > x) {
			y = x, x = d;
		}
		else if(d > y) {
			y = d;
		}
	}
} f[1 << 21];
void init(int n) {
	bit[0] = 1;
	for(int i = 1; i <= n; ++i) bit[i] = bit[i - 1] << 1;
}
int main() {
	init(21);
	read(n);
	for(int i = 1; i <= n; ++i) {
		read(a[i]);
		f[a[i]].update(i);
	}
	for(int i = 0; i < 21; ++i) {
		for(int s = 0; s < bit[21]; ++s) {
			if(s & bit[i]) {
				f[s ^ bit[i]].update(f[s].x);
				f[s ^ bit[i]].update(f[s].y);
			}
		}
	}
	int an = 0;
	for(int i = 1; i <= n; ++i) {
		int u = 0;
		for(int k = 20; ~k; --k) {
			if(a[i] & bit[k]) continue;
			if(f[u | bit[k]].y > i) u |= bit[k];
		}
		an = max(an, a[i] | u);
	}
	printf("%d\n", an);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char nc() {
	return getchar();
	static char buf[100000], *l = buf, *r = buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void read(T &x) {
	x = 0; int f = 1, ch = nc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
	x *= f;
}
const int maxn = 1e6 + 5;
const int maxnode = maxn * 16;
int n;
int a[maxn];
namespace tri {
	int all;
	int root;
	short siz[maxnode];
	int val[maxnode];
	int ch[maxnode][2];
	vector<int> temp;
	inline void newnode(int &u) {
		if(temp.size()) u = temp.back(), temp.pop_back();
		else u = ++all;
		siz[u] = 0;
		val[u] = 0;
		ch[u][0] = ch[u][1] = 0;
	}
	void remove(int u) {
		if(u == 0) return;
		temp.push_back(u);
		remove(ch[u][0]);
		remove(ch[u][1]);
	}
	void insert(int &u, int x, int k) {
		if(u == 0) newnode(u);
		siz[u] = min(siz[u] + 1, 10);
		if(k == -1) {
			return;
		}
		int c = bool(x & (1 << k));
		if(c == 1) insert(ch[u][c], x, k - 1);
		else {
			if(~val[u] & (1 << k)) insert(ch[u][c], x, k - 1);
		}
		if(siz[ch[u][1]] >= 2) {
			val[u] = (1 << k) | val[ch[u][1]];
			remove(ch[u][0]);
		}
		else {
			val[u] = max(val[ch[u][1]], val[ch[u][0]]);
		}
	}
}
int main() {
	read(n);
	for(int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	tri :: insert(tri :: root, a[n], 20);
	int an = 0;
	for(int i = n - 1; i > 1; --i) {
		tri :: insert(tri :: root, a[i], 20);
		an = max(an, a[i - 1] | tri :: val[tri :: root]);
	}
	printf("%d\n", an);
	return 0;
}


#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=3e6+10,mod=1e9+7,INF=0x3f3f3f3f;
int a[maxn],f[maxn];
void upd(int x,int k){
	if (f[x]>1)
		return;
	if (k<0)
		return;
	f[x]++;
	upd(x,k-1);
	if (x>>k&1)
		upd(x^(1<<k),k-1);
}
int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	int ans=0;
	for (int i=n;i;--i){
		int tmp=0;
		for (int j=20;j>=0;--j)
			if (!(a[i]>>j&1)&&f[tmp^(1<<j)]>1)
				tmp^=1<<j;
		if (i<=n-2)
			ans=max(ans,a[i]|tmp);
		upd(a[i],20);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=3e6+10,mod=1e9+7,INF=0x3f3f3f3f;
int a[maxn],f[maxn];
void upd(int x,int k){
	if (f[x]>1)
		return;
	if (k<0){
		f[x]++;
		return;
	}
	upd(x,k-1);
	if (x>>k&1)
		upd(x^(1<<k),k-1);
}
int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	int ans=0;
	for (int i=n;i;--i){
		int tmp=0;
		for (int j=20;j>=0;--j)
			if (!(a[i]>>j&1)&&f[tmp^(1<<j)]>1)
				tmp^=1<<j;
		if (i<=n-2)
			ans=max(ans,a[i]|tmp);
		upd(a[i],20);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=2e6+10,mod=1e9+7,INF=0x3f3f3f3f;
int a[maxn],f[maxn];
void upd(int x,int k){
	if (f[x]>1)
		return;
	if (k<0)
		return;
	f[x]++;
	upd(x,k-1);
	if (x>>k&1)
		upd(x^(1<<k),k-1);
}
int main()
{
	int n;
	cin>>n;
	for (int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	int ans=0;
	for (int i=n;i;--i){
		int tmp=0;
		for (int j=20;j>=0;--j)
			if (!(a[i]>>j&1)&&f[tmp^(1<<j)]>1)
				tmp^=1<<j;
		if (i<=n-2)
			ans=max(ans,a[i]|tmp);
		upd(a[i],20);
	}
	cout<<ans;
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define pii pair<int,int>
#define fir first
#define sec second
#define mk make_pair
#define pb push_back
#define ll long long
#define N 2107152
using namespace std;
inline int read()
{
	int neg=1,num=0;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')neg=-1;
	for(;isdigit(c);c=getchar())num=(num<<1)+(num<<3)+c-'0';
	return neg*num;
}
int n,ans,a[N],lg[N],g[N],f[N][2];
inline void Max(int x,int y)
{
	if(y>=f[x][0])f[x][1]=f[x][0],f[x][0]=y;
	else if(y>=f[x][1])f[x][1]=y;
}
inline void Solve(int w,int pos)
{
	int S=0;
    if(f[0][1]<=pos)return ;
	for(int i=20;i>=0;i--)
	{
		if(!(w>>i&1))continue;
		S|=(1<<i);
		if(f[S][1]<=pos)S^=(1<<i);
	}
	ans=max(ans,S|a[pos]);
}
signed main()
{
	n=read();int M=(1<<21)-1;
	for(int i=1;i<=n;i++)a[i]=read(),Max(a[i],i);
	for(int i=n;i>=1;i--)g[M-a[i]]=i;
	for(int i=1;i<=M;i++)lg[i]=(lg[i>>1]+1);
	for(int j=0;j<=20;j++)
		for(int i=M;i>=0;i--)
		{
			if((i>>j)&1)continue;
			int x=i|(1<<j);
			Max(i,f[x][0]);Max(i,f[x][1]);
		}
	for(int i=1;i<=n;i++)Solve(M-a[i],i);
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define pii pair<int,int>
#define fir first
#define sec second
#define mk make_pair
#define pb push_back
#define ll long long
#define N 2107152
using namespace std;
inline int read()
{
	int neg=1,num=0;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')neg=-1;
	for(;isdigit(c);c=getchar())num=(num<<1)+(num<<3)+c-'0';
	return neg*num;
}
int n,ans,a[N],lg[N],g[N],f[N][2];
inline void Max(int x,int y)
{
	if(y>=f[x][0])f[x][1]=f[x][0],f[x][0]=y;
	else if(y>=f[x][1])f[x][1]=y;
}
inline void Solve(int w,int pos)
{
	int S=0;
	for(int i=20;i>=0;i--)
	{
		if(!(w>>i&1))continue;
		S|=(1<<i);
		if(f[S][1]<=pos)S^=(1<<i);
	}
	ans=max(ans,S|a[pos]);
}
signed main()
{
	n=read();int M=(1<<21)-1;
	for(int i=1;i<=n;i++)a[i]=read(),Max(a[i],i);
	for(int i=n;i>=1;i--)g[M-a[i]]=i;
	for(int i=1;i<=M;i++)lg[i]=(lg[i>>1]+1);
	for(int j=0;j<=20;j++)
		for(int i=M;i>=0;i--)
		{
			if((i>>j)&1)continue;
			int x=i|(1<<j);
			Max(i,f[x][0]);Max(i,f[x][1]);
		}
	for(int i=1;i<=n;i++)Solve(M-a[i],i);
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define pii pair<int,int>
#define fir first
#define sec second
#define mk make_pair
#define pb push_back
#define ll long long
#define N 2107152
using namespace std;
inline int read()
{
	int neg=1,num=0;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')neg=-1;
	for(;isdigit(c);c=getchar())num=(num<<1)+(num<<3)+c-'0';
	return neg*num;
}
int n,ans,a[N],lg[N],g[N],f[N][2];
inline void Max(int x,int y)
{
	if(y>=f[x][0])f[x][1]=f[x][0],f[x][0]=y;
	else if(y>=f[x][1])f[x][1]=y;
}
signed main()
{
	n=read();int M=(1<<21)-1;
	for(int i=1;i<=n;i++)a[i]=read(),Max(a[i],i);
	for(int i=n;i>=1;i--)g[M-a[i]]=i;
	for(int i=1;i<=M;i++)lg[i]=(lg[i>>1]+1);
	for(int j=0;j<=20;j++)
		for(int i=M;i>=0;i--)
		{
			if((i>>j)&1)continue;
			int x=i|(1<<j);
			Max(i,f[x][0]);Max(i,f[x][1]);
		}
	for(int i=0;i<=M;i++)
		for(int j=i;j>0 && g[i];j=(j-1)&i)
		{
			if(f[j][1]>g[i]){ans=max(ans,(M-i)|j);break;}
		}
	cout<<ans;
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define pii pair<int,int>
#define fir first
#define sec second
#define mk make_pair
#define pb push_back
#define ll long long
#define N 2107152
using namespace std;
inline int read()
{
	int neg=1,num=0;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')neg=-1;
	for(;isdigit(c);c=getchar())num=(num<<1)+(num<<3)+c-'0';
	return neg*num;
}
int n,ans,a[N],lg[N],g[N],f[N][2];
inline void Max(int x,int y)
{
	if(y>=f[x][0])f[x][1]=f[x][0],f[x][0]=y;
	else if(y>=f[x][1])f[x][1]=y;
}
inline void Solve(int w,int pos)
{
	int S=0;
	if(f[0][1]<=pos)return ;
	for(int i=20;i>=0;i--)
	{
		if(!(w>>i&1))continue;
		S|=(1<<i);
		if(f[S][1]<=pos)S^=(1<<i);
	}
	ans=max(ans,S|a[pos]);
}
signed main()
{
	n=read();int M=(1<<21)-1;
	for(int i=1;i<=n;i++)a[i]=read(),Max(a[i],i);
	for(int i=n;i>=1;i--)g[M-a[i]]=i;
	for(int i=1;i<=M;i++)lg[i]=(lg[i>>1]+1);
	for(int j=0;j<=20;j++)
		for(int i=M;i>=0;i--)
		{
			if((i>>j)&1)continue;
			int x=i|(1<<j);
			Max(i,f[x][0]);Max(i,f[x][1]);
		}
	for(int i=1;i<=n;i++)Solve(M-a[i],i);
	printf("%d",ans);
	return 0;
}
/*
3
1 5 15
0001
0101
1111
*/


#include<bits/stdc++.h>

using namespace std;

const long long MAXN = 2000500;
const long long INF = 0x7f3f3f3f;
const long long LINF = 0x1f3f3f3f3f3f3f3f;
const long long MOD = 998244853;
const long long OVER_FLOW = 0xffffffff;

long long n, m; /**< n个数,m个操作,以model为模 */
long long buf[MAXN];
long long dp[MAXN];

int a[MAXN],f[MAXN][21],A[MAXN];
void add(int x,int p)
{
    if(p==-1) {if(A[x]!=2) A[x]++;return;}
    if(f[x][p]==2) return;
    f[x][p]++;
    add(x,p-1);
    if((x>>p)&1) add(x^(1<<p),p-1);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

/*
写代码时请注意：
    1.是否要开Long Long？数组边界处理好了么？
    2.实数精度
    3.下标能从最小值开始就从最小值开始
    5.排序之前不能取模.
    6.0-1子矩阵子序列:前缀和
    7.模拟题注意代码复用.
    8.单个区间问题考虑前缀和,多个区间左右端点拆开,排序,记录端点极值
思考提醒：
    1.最大值和最小值问题可不可以用二分+贪心？
    2.dp
    3.如果规模小的话还能尝试dfs
    4.如果存在等式.转换关系少可以暴力枚举变量,或者考虑从数据大小入手
*/

    ios::sync_with_stdio(false);
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> buf[i];
    long long res = 0;
    for (long long i = n; i >= 1; i--) {
        long long t = 0;
        if (i <= n - 2) {
            for (long long j = 20; j >= 0; j--) {
                if (buf[i] & (1 << j)) {
                    continue;
                } else if (A[t | (1 << j)] >= 2) {
                    t |= (1 << j);
                }
            }
        }
        res = max(res, t | buf[i]);
        add(buf[i], 20);
    }
    cout << res << endl;

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

const long long MAXN = 2000500;
const long long INF = 0x7f3f3f3f;
const long long LINF = 0x1f3f3f3f3f3f3f3f;
const long long MOD = 998244853;
const long long OVER_FLOW = 0xffffffff;

long long n, m; /**< n个数,m个操作,以model为模 */
long long buf[MAXN];
long long dp[MAXN];

void insert(long long x, long long k) {
    if (k == -1) {
        if (dp[x] != 2) dp[x]++;
        return;
    }
    if (dp[x] >= 2) return;
    dp[x]++;
    insert(x, k - 1);
    if ((x >> k) & 1) insert(x ^ (1 << k), k - 1);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

/*
写代码时请注意：
    1.是否要开Long Long？数组边界处理好了么？
    2.实数精度
    3.下标能从最小值开始就从最小值开始
    5.排序之前不能取模.
    6.0-1子矩阵子序列:前缀和
    7.模拟题注意代码复用.
    8.单个区间问题考虑前缀和,多个区间左右端点拆开,排序,记录端点极值
思考提醒：
    1.最大值和最小值问题可不可以用二分+贪心？
    2.dp
    3.如果规模小的话还能尝试dfs
    4.如果存在等式.转换关系少可以暴力枚举变量,或者考虑从数据大小入手
*/

    ios::sync_with_stdio(false);
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> buf[i];
    long long res = 0;
    for (long long i = n; i >= 1; i--) {
        long long t = 0, cur = 0;
        if (i <= n - 2) {
            for (long long j = 20; j >= 0; j--) {
                if (buf[i] & (1 << j)) {
                    cur |= (1 << j);
                    continue;
                } else if (dp[t | (1 << j)] >= 2) {
                    t |= (1 << j);
                    cur |= (1 << j);
                }
            }
        }
        res = max(res, cur);
        insert(buf[i], 20);
    }
    cout << res << endl;

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

const long long MAXN = 2000500;
const long long INF = 0x7f3f3f3f;
const long long LINF = 0x1f3f3f3f3f3f3f3f;
const long long MOD = 998244853;
const long long OVER_FLOW = 0xffffffff;

long long n, m; /**< n个数,m个操作,以model为模 */
long long buf[MAXN];
long long dp[MAXN];

void insert(long long x, long long k) {
    if (k == -1) {
        if (dp[x] != 2) dp[x]++;
        return;
    }
    if (dp[x] >= 2) return;
    dp[x]++;
    insert(x, k - 1);
    if ((x >> k) % 1) insert(x ^ (1 << k), k - 1);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

/*
写代码时请注意：
    1.是否要开Long Long？数组边界处理好了么？
    2.实数精度
    3.下标能从最小值开始就从最小值开始
    5.排序之前不能取模.
    6.0-1子矩阵子序列:前缀和
    7.模拟题注意代码复用.
    8.单个区间问题考虑前缀和,多个区间左右端点拆开,排序,记录端点极值
思考提醒：
    1.最大值和最小值问题可不可以用二分+贪心？
    2.dp
    3.如果规模小的话还能尝试dfs
    4.如果存在等式.转换关系少可以暴力枚举变量,或者考虑从数据大小入手
*/

    ios::sync_with_stdio(false);
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> buf[i];
    long long res = 0;
    for (long long i = n; i >= 1; i--) {
        long long t = 0, cur = 0;
        if (i <= n - 2) {
            for (long long j = 20; j >= 0; j--) {
                if (buf[i] & (1 << j)) {
                    cur |= (1 << j);
                    continue;
                } else if (dp[t | (1 << j)] >= 2) {
                    t |= (1 << j);
                    cur |= (1 << j);
                }
            }
        }
        res = max(res, cur);
        insert(buf[i], 20);
    }
    cout << res << endl;

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

const long long MAXN = 2000500;
const long long INF = 0x7f3f3f3f;
const long long LINF = 0x1f3f3f3f3f3f3f3f;
const long long MOD = 998244853;
const long long OVER_FLOW = 0xffffffff;

long long n, m; /**< n个数,m个操作,以model为模 */
long long buf[MAXN];
long long dp[MAXN];

void insert(long long x, long long k) {
    if (k == -1) {
        if (dp[x] != 2) dp[x]++;
        return;
    }
    if (dp[x] >= 2) return;
    dp[x]++;
    insert(x, k - 1);
    if ((x >> k) & 1) insert(x ^ (1 << k), k - 1);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

/*
写代码时请注意：
    1.是否要开Long Long？数组边界处理好了么？
    2.实数精度
    3.下标能从最小值开始就从最小值开始
    5.排序之前不能取模.
    6.0-1子矩阵子序列:前缀和
    7.模拟题注意代码复用.
    8.单个区间问题考虑前缀和,多个区间左右端点拆开,排序,记录端点极值
思考提醒：
    1.最大值和最小值问题可不可以用二分+贪心？
    2.dp
    3.如果规模小的话还能尝试dfs
    4.如果存在等式.转换关系少可以暴力枚举变量,或者考虑从数据大小入手
*/

    ios::sync_with_stdio(false);
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> buf[i];
    long long res = 0;
    for (long long i = n; i >= 1; i--) {
        long long t = 0;
        if (i <= n - 2) {
            for (long long j = 20; j >= 0; j--) {
                if (buf[i] & (1 << j)) {
                    continue;
                } else if (dp[t | (1 << j)] >= 2) {
                    t |= (1 << j);
                }
            }
        }
        res = max(res, t | buf[i]);
        insert(buf[i], 20);
    }
    cout << res << endl;

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

using namespace std;

const long long MAXN = 2000500;
const long long INF = 0x7f3f3f3f;
const long long LINF = 0x1f3f3f3f3f3f3f3f;
const long long MOD = 998244853;
const long long OVER_FLOW = 0xffffffff;

long long n, m; /**< n个数,m个操作,以model为模 */
long long buf[MAXN];
long long dp[MAXN];

int a[MAXN], f[MAXN][21], A[MAXN];

void add(int x, int p) {
    if (p == -1) {
        if (A[x] != 2) A[x]++;
        return;
    }
    if (f[x][p] == 2) return;
    f[x][p]++;
    add(x, p - 1);
    if ((x >> p) & 1) add(x ^ (1 << p), p - 1);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

/*
写代码时请注意：
    1.是否要开Long Long？数组边界处理好了么？
    2.实数精度
    3.下标能从最小值开始就从最小值开始
    5.排序之前不能取模.
    6.0-1子矩阵子序列:前缀和
    7.模拟题注意代码复用.
    8.单个区间问题考虑前缀和,多个区间左右端点拆开,排序,记录端点极值
思考提醒：
    1.最大值和最小值问题可不可以用二分+贪心？
    2.dp
    3.如果规模小的话还能尝试dfs
    4.如果存在等式.转换关系少可以暴力枚举变量,或者考虑从数据大小入手
*/

    ios::sync_with_stdio(false);
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> buf[i];
    long long res = 0;
    for (long long i = n; i >= 1; i--) {
        if (i <= n - 2) {
            long long t = 0;
            for (long long j = 20; j >= 0; j--) {
                if (buf[i] & (1 << j)) {
                    continue;
                } else if (A[t | (1 << j)] >= 2) {
                    t |= (1 << j);
                }
            }
            res = max(res, t | buf[i]);
        }
        add(buf[i], 20);
    }
    cout << res << endl;

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int ha,n,mx[2500010],a[1000010],cmx[2500010],ans;
void hb(int i,int j){
	if(mx[j]>=mx[i]){
		cmx[i]=max(mx[i],cmx[j]);
		mx[i]=mx[j];
	}
	 else if(mx[j]>cmx[i])cmx[i]=mx[j];
}
int main(){
	int i,j,now,la;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	ha=1<<21;
	for(i=n;i;i--){
		if(!mx[a[i]])mx[a[i]]=i;
		 else if(!cmx[a[i]])cmx[a[i]]=i;
	}
	for(i=0;i<21;i++)
	 for(j=0;j<ha;j++)if(!(j&(1<<i)))hb(j,j|(1<<i));
	for(i=1;i<=n;i++){
		now=0;la=0;
		for(j=20;j>=0;j--){
		 if(a[i]&(1<<j))now+=1<<j;
		 else{
		 	 if(cmx[la|(1<<j)]>i){
			   la+=1<<j,now+=1<<j;
			   //printf("%d %d %d %d\n",i,j,la,now);
		   }
		}
	}
		//printf("%d\n",now);
		ans=max(ans,now);
	}
	printf("%d",ans);
}


#include<iostream>
#include<algorithm>
using namespace std;

int A[1<<21];
void upd(int x){
	if(A[x]<2)A[x]++;
	else return;
	for(int i=0;i<=20;i++){
		if(x&(1<<i)) upd(x^(1<<i));
	}
}

int a[1 << 21];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	int ans=0;
	for(int i=n-1;i>=0;i--){
		int now=0;
		for(int j=20;j>=0;j--){
			if((a[i]&(1<<j))==0){
				now|=A[now|(1<<j)]>=2?1<<j:0;
			}
		}
		// cout<<i<<" "<<a[i]<<" "<<now<<endl;
		// for(int i=1;i<=10;i++)cout<<A[i]<<" ";cout<<endl;
		if(i<n-2)ans=max(ans,a[i]|now);
		upd(a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
using namespace std;

pair<int,int> A[1<<21];
// tag为序号,不能为0
void upd(int x,int tag){
	if(A[x].second||A[x].first==tag||A[x].second==tag) return;
	else (A[x].first?A[x].second:A[x].first)=tag;
	for(int i=0;i<=20;i++){
		if(x&(1<<i)) upd(x^(1<<i),tag);
	}
}

int a[1 << 21];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; i++) cin >> a[n-1-i];
	int ans=0;
	for(int i=0;i<n;i++){
		int now=0;
		for(int j=20;j>=0;j--){
			if((a[i]&(1<<j))==0){
				now|=A[now|(1<<j)].second?1<<j:0;
			}
		}
		// cout<<i<<" "<<a[i]<<" "<<now<<endl;
		// for(int i=1;i<=20;i++)cout<<A[i].first<<","<<A[i].second<<" ";cout<<endl;
		if(i>=2)ans=max(ans,a[i]|now);
		upd(a[i],i+1);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
using namespace std;

int A[1<<21];
void upd(int x,int k) {
    if(A[x|k]==2) return;
    if(x==0){
		A[k]++;
		return;
	}
	upd(x&(x-1),k|(x&(-x)));
    upd(x&(x-1),k);
}
int a[1 << 21];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[n+1-i];
	int ans=0;
	int();
	for(int i=1;i<=n;i++){
		int now=0;
		for(int j=20;j>=0;j--){
			if((a[i]&(1<<j))==0){
				now|=A[now|(1<<j)]==2?1<<j:0;
			}
		}
		if(i>=3)ans=max(ans,a[i]|now);
		upd(a[i],0);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
using namespace std;

int A[1<<21];
void upd(int x, int k) {
    if(A[x | k] == 2) return;
    if(x == 0) {
        A[k]++;
        return;
    }
	// x&(x-1) 去掉最右侧的1
    upd(x & (x - 1), k | (x & (-x)));
    upd(x & (x - 1), k);
}
int a[1 << 21];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[n+1-i];
	int ans=0;
	int();
	for(int i=1;i<=n;i++){
		int now=0;
		for(int j=20;j>=0;j--){
			if((a[i]&(1<<j))==0){
				now|=A[now|(1<<j)]==2?1<<j:0;
			}
		}
		if(i>=3)ans=max(ans,a[i]|now);
		upd(a[i],0);
	}
	cout<<ans<<endl;
}


// https://www.luogu.com.cn/problem/P4717
#include<iostream>
#include<algorithm>
using namespace std;
const int mod = 998244353;
#define SFAdd(x,y)	((x+=y)<mod?x:x-=mod)
#define SFSub(x,y)	((x-=y)<0?x+=mod:x)
#define Add(a,x,y)	((a=x+y)<mod?a:a-=mod)
#define Sub(a,x,y)	((a=x-y)<0?a+=mod:a)
#define Div2(x)	(x&1?(x+=mod)>>=1:x>>=1)

void FWT_and(int a[], int n, int on) {
	for (int i = 1; i < n; i <<= 1)
		for (int j = 0; j < n; j += i << 1)
			for (int k = j; k < i + j; k++)
				on == 1 ? SFAdd(a[k], a[i + k]) : SFSub(a[k], a[i + k]);
}

int A[1<<21];
void upd(int x){
	if(A[x]<2)A[x]++;
	else return;
	for(int i=0;i<=20;i++){
		if(x&(1<<i)) upd(x^(1<<i));
	}
}

int a[1 << 22];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	int ans=0;
	for(int i=n-1;i>=0;i--){
		int now=0;
		for(int j=20;j>=0;j--){
			if((a[i]&(1<<j))==0){
				now|=A[now|(1<<j)]>=2?1<<j:0;
			}
		}
		// cout<<i<<" "<<a[i]<<" "<<now<<endl;
		// for(int i=1;i<=10;i++)cout<<A[i]<<" ";cout<<endl;
		ans=max(ans,a[i]|now);
		upd(a[i]);
	}
	cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define mp(x,y) make_pair(x,y)
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;

inline int rd()
{
	int xx=0,ff=1;
	char ch=getchar();
	while(ch<'0' or ch>'9'){if(ch=='-')ff=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){xx=xx*10-'0'+ch;ch=getchar();}
	return xx*ff;
}

const int N=1e6+10;

int n;
int a[N];
int f[(1<<21)][21];

void update(int s,int k)
{
	if(k > 20)return ;
	if(f[s][k] >= 2)return ;

	f[s][k]++;

	update(s , k + 1);
	if(s & (1 << k) )
		update(s ^ (1 << k) , k);
}

int ans = 0;

int main()
{
	n = rd();
	for(int i=1;i<=n;i++)a[i] = rd();

//	update(a[n] , 0);
	for(int i=n-1;i;i--)
	{
		int tmp = 0,now = 0;
		if(i < n - 1)
		{
			for(int j=20;j>=0;j--)
			{
				if(a[i] & (1<<j))tmp |= (1<<j);
				else if(f[now | (1<<j)][20] >= 2)
					tmp |= (1<<j),now |= (1<<j);
			}
		}
		update(a[i] , 0);
		ans = max(ans , tmp);
	}

	printf("%d",ans);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define mp(x,y) make_pair(x,y)
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;

inline int rd()
{
	int xx=0,ff=1;
	char ch=getchar();
	while(ch<'0' or ch>'9'){if(ch=='-')ff=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){xx=xx*10-'0'+ch;ch=getchar();}
	return xx*ff;
}

const int N=1e6+10;

int n;
int a[N];
int f[(1<<21)][21];

void update(int s,int k)
{
	if(k > 20)return ;
	if(f[s][k] >= 2)return ;

	f[s][k]++;

	update(s , k + 1);
	if(s & (1 << k) )
		update(s ^ (1 << k) , k);
}

int ans;

int main()
{
	n = rd();
	for(int i=1;i<=n;i++)a[i] = rd();

	update(a[n] , 0);
	for(int i=n-1;i;i--)
	{
		int tmp = 0,now = 0;
		for(int j=20;j>=0;j--)
		{
			if(a[i] & (1<<j))tmp |= (1<<j);
			else if(f[now | (1<<j)][20] >= 2)
				tmp |= (1<<j),now |= (1<<j);
		}
		update(a[i] , 0);
		ans = max(ans , tmp);
	}

	printf("%d",ans);

	return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 2e6 + 5;

int n, ans;
int a[N];
int f[(1 << 21)][21];//f[S][i]为是S子集且

void modify(int s, int k) {
	if (k > 20) return;
	if (f[s][k] >= 2) return;
	f[s][k] += 1;

	modify(s, k + 1);
	if (s & (1 << k))
		modify(s ^ (1 << k), k);
}

int main() {
//	freopen("trie.in", "r", stdin);
//	freopen("trie.out", "w", stdout);
	scanf("%d\n", &n);
	for (int i = 1; i <= n; ++i)
		scanf("%d", a + i);

	for (int i = n; i; --i) {
		int tmp = 0, now = 0;
		if (i < n - 1) {
			for (int j = 20; ~j; --j)
				if (a[i] & (1 << j))
					tmp |= (1 << j);
				else
					if (f[now | (1 << j)][20] >= 2)
						tmp |= (1 << j), now |= (1 << j);
		}
		modify(a[i], 0);
		ans = max(ans, tmp);
	}
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define mp(x,y) make_pair(x,y)
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;

inline int rd()
{
	int xx=0,ff=1;
	char ch=getchar();
	while(ch<'0' or ch>'9'){if(ch=='-')ff=-1;ch=getchar();}
	while(ch>='0' and ch<='9'){xx=xx*10-'0'+ch;ch=getchar();}
	return xx*ff;
}

const int N=1e6+10;

int n;
int a[N];
int f[(1<<21)][21];

void update(int s,int k)
{
	if(k > 20)return ;
	if(f[s][k] >= 2)return ;

	f[s][k]++;

	update(s , k + 1);
	if(s & (1 << k) )
		update(s ^ (1 << k) , k);
}

int ans = 0;

int main()
{
	n = rd();
	for(int i=1;i<=n;i++)a[i] = rd();

//	update(a[n] , 0);
	for(int i=n;i;i--)
	{
		int tmp = 0,now = 0;
		if(i < n - 1)
		{
			for(int j=20;j>=0;j--)
			{
				if(a[i] & (1<<j))tmp |= (1<<j);
				else if(f[now | (1<<j)][20] >= 2)
					tmp |= (1<<j),now |= (1<<j);
			}
		}
		update(a[i] , 0);
		ans = max(ans , tmp);
	}

	printf("%d",ans);

	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

int mask_cnt[MAXN][2];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (mask_cnt[mask|i][1] != -1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (mask_cnt[X][0] == -1 || mask_cnt[X][0] == V) {
    mask_cnt[X][0] = V;
  } else {
    mask_cnt[X][1] = V;
  }
  if (mask_cnt[X][1] == -1) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  CLR (mask_cnt, -1);

  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

set<int> mask_cnt[MAXN];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (sz(mask_cnt[mask|i]) > 1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (sz(mask_cnt[X]) > 1) {
    return;
  }
  if (mask_cnt[X].find(V) != mask_cnt[X].end()) {
    return;
  }

  mask_cnt[X].insert(V);
  if (sz(mask_cnt[X]) <= 2) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  int ans = 0;

  fill_mask_cnt(vet[n-1], n-1);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], i+1);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

set<int> mask_cnt[MAXN];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (sz(mask_cnt[mask|i]) > 1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (mask_cnt[X].find(V) != mask_cnt[X].end()) {
    return;
  }

  mask_cnt[X].insert(V);
  if (sz(mask_cnt[X]) <= 2) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

int mask_cnt[MAXN][2];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (mask_cnt[mask|i][1] != -1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (mask_cnt[X][0] == -1) {
    mask_cnt[X][0] = V;
  } else {
    mask_cnt[X][1] = V;
  }
  if (mask_cnt[X][1] == -1) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  CLR (mask_cnt, -1);

  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

int mask_cnt[MAXN][2];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (mask_cnt[mask|i][1] != -1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (mask_cnt[X][0] == -1 || mask_cnt[X][0] == V) {
    mask_cnt[X][0] = V;
  }
  if (mask_cnt[X][1] == -1) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }

  if (mask_cnt[X][1] == -1) {
    mask_cnt[X][1] = V;
  }
}

int vet[MAXN];

int solve(int n) {
  CLR (mask_cnt, -1);

  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 3000006;

set<int> mask_cnt[MAXN];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (sz(mask_cnt[mask|i]) > 1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  mask_cnt[X].insert(V);
  if (sz(mask_cnt[X]) <= 2) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define CLR(x, a) memset(x, a, sizeof(x))
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo

using namespace std;

#define ii pair<int,int>
#define ll long long

const int MAXN = 2500006;

set<int> mask_cnt[MAXN];

int find_sol (int X) {
  int mask = 0;
  for (int i = 1<<21; i > 0; i >>= 1) {
    if (X&i) {
      continue;
    }

    if (sz(mask_cnt[mask|i]) > 1) {
      mask |= i;
    }
  }
  return X | mask;
}

void fill_mask_cnt (int X, int V) {
  if (sz(mask_cnt[X]) > 1) {
    return;
  }
  if (mask_cnt[X].find(V) != mask_cnt[X].end()) {
    return;
  }

  mask_cnt[X].insert(V);
  if (sz(mask_cnt[X]) <= 2) {
    for (int i = 1<<21; i > 0; i >>= 1) {
      if (X&i) {
        fill_mask_cnt(X^i, V);
      }
    }
  }
}

int vet[MAXN];

int solve(int n) {
  int ans = 0;

  fill_mask_cnt(vet[n-1], vet[n-1]);
  for (int i = n-3; i >= 0; i--) {
    fill_mask_cnt(vet[i+1], vet[i+1]);
    ans = max(ans, find_sol(vet[i]));

    // cerr << i << " " << vet[i] << " " << find_sol(vet[i]) << endl;
  }
  return ans;
}

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin>>n;
  FORR (i, n) {
    cin>>vet[i];
  }

  cout << solve (n) << endl;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
    int n,p[100002];
    char s[100002];
    int a[100002],b[100002];
    long long ans=0,res=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>p[i];
    for(int i=0;i<n;i++)
        cin>>s[i];
    if(s[0]=='B')
        a[0]=p[0];
    else
        a[0]=0;
    ans=a[0];
    for(int i=1;i<n;i++)
    {
        a[i]=a[i-1];
        if(s[i]=='B')
            a[i]+=p[i];
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            res+=p[i];
            ans=max(ans,a[i-1]+res);
        }
    }
    if(s[0]=='A')
        ans=max(ans,res+p[0]);
    if(s[n-1]=='B')
        a[n-1]=p[n-1];
    else
        a[n-1]=0;
    for(int i=n-2;i>=0;i--)
    {
        a[i]=a[i+1];
        if(s[i]=='B')
            a[i]+=p[i];
    }
    res=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            res+=p[i];
            ans=max(ans,a[i+1]+res);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
    int n,p[100002];
    char s[100002];
    int a[100002];
    long long ans=0,res=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>p[i];
    for(int i=0;i<n;i++)
        cin>>s[i];
    if(s[0]=='B')
        a[0]=p[0];
    else
        a[0]=0;
    for(int i=1;i<n;i++)
    {
        a[i]=a[i-1];
        if(s[i]=='B')
            a[i]+=p[i];
    }
    ans=a[n-1];
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='A')
        {
            res+=p[i];
            ans=max(ans,a[i-1]+res);
        }
    }
    if(s[n-1]=='B')
        a[n-1]=p[n-1];
    else
        a[n-1]=0;
    for(int i=n-2;i>=0;i--)
    {
        a[i]=a[i+1];
        if(s[i]=='B')
            a[i]+=p[i];
    }
    res=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            res+=p[i];
            ans=max(ans,a[i+1]+res);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,f[10000],a=0,b=0,tmp,sum=0;
    char s[10000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
    cout<<max;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    long long int n,f[500000],a=0,b=0,tmp,sum=0;
    char s[500000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a,max2=sum;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }

    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

       max=max1;
    }
    else
        max=max;

        if(max<max2)
        {
            cout<<max2;
        }
        else
            cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    long long int n,f[100000],a=0,b=0,tmp,sum=0;
    char s[100000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

        cout<<max1;
    }
    else
        cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    long long int n,f[500000],a=0,b=0,tmp,sum=0;
    char s[500000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

        cout<<max1;
    }
    else
        cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    long long int n,f[500000],a=0,b=0,tmp,sum=0;
    char s[500000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",f[i]);
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

        cout<<max1;
    }
    else
        cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,f[100000],a=0,b=0,tmp,sum=0;
    char s[100000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

        cout<<max1;
    }
    else
        cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    long long int n,f[500000],a=0,b=0,tmp,sum=0;
    char s[500000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a,max2=sum;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }

    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

       max=max1;
    }
    else
        max=max1;

        if(max<max2)
        {
            cout<<max2;
        }
        else
            cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,f[100000],a=0,b=0,tmp,sum=0;
    char s[100000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i];
    }
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max=a;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=0;i<n-1;i++)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max)
               {

                max=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max)
                {max=tmp;}

        }

    }
  a=0;
     for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            a=a+f[i];

        }
        else if(s[i]=='B')
        {
            sum=sum+f[i];
        }
    }

    long long int max1=a;

     b=0;
    if(a==0)
    {
        cout<<sum;
        return 0;
    }
    for(int i=n-1;i>0;i--)
    {
        if(s[i]=='A')
        {
            a=a-f[i];
            tmp=b+a;
            if(tmp>max1)
               {

                max1=tmp;
               }

        }
        else if(s[i]=='B')
        {
            b=b+f[i];
            tmp=b+a;
            if(tmp>max1)
                {max1=tmp;}

        }

    }
    if(max<max1)
    {

        cout<<max1;
    }
    else
        cout<<max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{int n,a[100000],sum1;
char s[100000];
cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];
}
cin>>s;
int sum=0;
for(int i=0;i<n;i++)
{
if(s[i]=='B')
{sum=sum+a[i];
}
}
int x=sum;
int max=sum;
for(int i=0;i<n;i++)
{
if(s[i]=='A')
{
sum=sum+a[i];
if(sum>max)
max=sum;

sum1=x+a[i];
if(sum1>max)
max=sum1;
}
if(s[i]=='B')
{
sum=sum-a[i];
if(sum>max)
max=sum;
}

}
cout<<max;
return 0;}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
string s;
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	cin>>&s[1];
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else{
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	string s;
	scanf("%s",&s[1]);
	for(int  i=1;i<=n;i++)
	{
		if(s[i]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else{
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
string s;
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	cin>>s;
	for(int i=1;i<=n;i++)
	{
		if(s[i-1]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else{
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
string s;
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	scanf("%s",&s[1]);
	for(int  i=1;i<=n;i++)
	{
		if(s[i]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else{
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
string s;
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	cin>>&s[1];
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else if(s[i]=='B'){
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=5555555;
typedef long long ll;
ll x[maxn];
ll a[maxn],b[maxn];
string s;
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i];
	cin>>s;
	for(int i=1;i<=n;i++)
	{
		if(s[i-1]=='A'){
			a[i]=a[i-1]+x[i];
			b[i]=b[i-1];
		}
		else if(s[i-1]=='B'){
			b[i]=b[i-1]+x[i];
			a[i]=a[i-1];
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,a[i]+b[n]-b[i]);
		ans=max(ans,b[i]+a[n]-a[i]);
	}
	cout<<ans<<endl;
}


/*************************************
**----------------------------------**
*|**********************************|*
*|*  CODE By : Mohd. Ausaf Jafri   *|*
*|*     ECE, MNNIT , Allahabad     *|*
*|*                                *|*
*|*      ausafjafri@gmail.com      *|*
*|*   "Think Twice, Code Once"     *|*
*|**********************************|*
**----------------------------------**
**************************************/

/******** HEADER FILES ****************/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

/******* STL & LOOPS ************************/

#define TESTCASE int tt; ind(tt) while(tt--)
#define upto(i,n) for(int i=0;i<n;i++)
#define from(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

/************* CONSTANTS ********************/

const long long int inf = 1e17 + 110;
const int mod = 1e9 + 7;
const int MAX = 1e6 + 3;

/******* INPUTS-ALL KIND ********************/

#define inc(c) scanf("%c",&c);
#define ins(s) scanf("%s",s);
#define ind(n) scanf("%d",&n);
#define inlld(n) scanf("%lld",&n);
#define ind2(n,m) scanf("%d%d",&n,&m);
#define inlld2(n,m) scanf("%lld%lld",&n,&m);

/******* OUTPUTS-ALL KIND *********************/

#define opc(c) printf("%c\n",c);
#define ops(s) printf("%s\n",s);
#define opd(n) printf("%d\n",n);
#define oplld(n) printf("%lld\n",n);
#define opd2(n,m) printf("%d %d\n",n,m);
#define oplld2(n,m) printf("%lld %lld\n",n,m);

/******* ALIASING ***********************/

typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ll> vll;
typedef pair<int, int> ii; //pair
typedef vector<ii> vii;   //vector of pair
typedef vector< pair<int, ii> > edgelist; //maintain edge list

/********** USER DEFINED FUNCTIONS *******/

int isprime(int N){
    if(N<2 || (!(N&1) && N!=2))
        return 0;
    for(int i=3; i*i<=N; i+=2){
        if(!(N%i))
            return 0;
    }
    return 1;
}

int power(int a,int b)
{
    int res=1;
    a=a%mod;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%mod;
        b=b>>1;
        a=(a*a)%mod;
    }
    return res;
}

/********** LOGIC STARTS HERE ************/

int main()
{
   string str;
   int n;
   ind(n)
   ll arr[n],sum1=-1,ans1=0,ans2=0,sum2=-1,back[n],front[n];
    memset(back,0,sizeof(back));
    memset(front,0,sizeof(front));
    upto(i,n)
        inlld(arr[i])
        cin>>str;
    bool ok=true;
    upto(i,n)
    {
        if(str[i]!='B')
        {
            ok=false;
            break;
        }
    }
    if(ok)
    {
        upto(i,n)
            ans1+=arr[i];
        oplld(ans1)
    }
    else
    if(n!=1)
    {
    upto(i,n)
    {
        if(str[i]=='B')
            front[i]+=arr[i]+front[i-1];
        else
        {   if(i-1>=0)
            front[i]=front[i-1];}
    }
    rev(i,n-1,0)
    {
        if(str[i]=='B')
            back[i]+=arr[i]+back[i+1];
        else
        {if(i+1<n)
            back[i]=back[i+1];}
    }
    upto(i,n)
    {
        if(str[i]=='A')
            ans1+=arr[i];
        if(i+1 < n)
            ans1+=back[i+1];
        sum1=max(sum1,ans1);
        ans1-=back[i+1];
    }

    rev(i,n-1,0)
    {
        if(str[i]=='A')
            ans2+=arr[i];
        if(i-1 >=0)
            ans2+=front[i-1];
        sum2=max(sum2,ans2);
        ans2-=front[i-1];
    }
    oplld(max(sum1,sum2))
    }
    else

    {
        oplld(arr[0])
    }
}


/*************************************
**----------------------------------**
*|**********************************|*
*|*  CODE By : Mohd. Ausaf Jafri   *|*
*|*     ECE, MNNIT , Allahabad     *|*
*|*                                *|*
*|*      ausafjafri@gmail.com      *|*
*|*   "Think Twice, Code Once"     *|*
*|**********************************|*
**----------------------------------**
**************************************/

/******** HEADER FILES ****************/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

/******* STL & LOOPS ************************/

#define TESTCASE int tt; ind(tt) while(tt--)
#define upto(i,n) for(int i=0;i<n;i++)
#define from(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

/************* CONSTANTS ********************/

const long long int inf = 1e17 + 110;
const int mod = 1e9 + 7;
const int MAX = 1e6 + 3;

/******* INPUTS-ALL KIND ********************/

#define inc(c) scanf("%c",&c);
#define ins(s) scanf("%s",s);
#define ind(n) scanf("%d",&n);
#define inlld(n) scanf("%lld",&n);
#define ind2(n,m) scanf("%d%d",&n,&m);
#define inlld2(n,m) scanf("%lld%lld",&n,&m);

/******* OUTPUTS-ALL KIND *********************/

#define opc(c) printf("%c\n",c);
#define ops(s) printf("%s\n",s);
#define opd(n) printf("%d\n",n);
#define oplld(n) printf("%lld\n",n);
#define opd2(n,m) printf("%d %d\n",n,m);
#define oplld2(n,m) printf("%lld %lld\n",n,m);

/******* ALIASING ***********************/

typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ll> vll;
typedef pair<int, int> ii; //pair
typedef vector<ii> vii;   //vector of pair
typedef vector< pair<int, ii> > edgelist; //maintain edge list

/********** USER DEFINED FUNCTIONS *******/

int isprime(int N){
    if(N<2 || (!(N&1) && N!=2))
        return 0;
    for(int i=3; i*i<=N; i+=2){
        if(!(N%i))
            return 0;
    }
    return 1;
}

int power(int a,int b)
{
    int res=1;
    a=a%mod;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%mod;
        b=b>>1;
        a=(a*a)%mod;
    }
    return res;
}

/********** LOGIC STARTS HERE ************/

int main()
{
   string str;
   int n;
   ind(n)
   ll arr[n],sum=0;

    upto(i,n)
        inlld(arr[i])
        cin>>str;
    upto(i,n)
    {
        if(str[i]=='B')
            sum+=arr[i];
    }
    ll ans1=sum;
    ll ans2=sum;
     upto(i,n)
    {
        if(str[i]=='A')
            ans1+=arr[i];
        else
            ans1-=arr[i];
        sum=max(sum,ans1);

    }

    rev(i,n-1,0)
    {
        if(str[i]=='A')
            ans2+=arr[i];
         else
            ans2-=arr[i];
        sum=max(sum,ans2);

    }
    oplld(sum)

}


/*************************************
**----------------------------------**
*|**********************************|*
*|*  CODE By : Mohd. Ausaf Jafri   *|*
*|*     ECE, MNNIT , Allahabad     *|*
*|*                                *|*
*|*      ausafjafri@gmail.com      *|*
*|*   "Think Twice, Code Once"     *|*
*|**********************************|*
**----------------------------------**
**************************************/

/******** HEADER FILES ****************/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

/******* STL & LOOPS ************************/

#define TESTCASE int tt; ind(tt) while(tt--)
#define upto(i,n) for(int i=0;i<n;i++)
#define from(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

/************* CONSTANTS ********************/

const long long int inf = 1e17 + 110;
const int mod = 1e9 + 7;
const int MAX = 1e6 + 3;

/******* INPUTS-ALL KIND ********************/

#define inc(c) scanf("%c",&c);
#define ins(s) scanf("%s",s);
#define ind(n) scanf("%d",&n);
#define inlld(n) scanf("%lld",&n);
#define ind2(n,m) scanf("%d%d",&n,&m);
#define inlld2(n,m) scanf("%lld%lld",&n,&m);

/******* OUTPUTS-ALL KIND *********************/

#define opc(c) printf("%c\n",c);
#define ops(s) printf("%s\n",s);
#define opd(n) printf("%d\n",n);
#define oplld(n) printf("%lld\n",n);
#define opd2(n,m) printf("%d %d\n",n,m);
#define oplld2(n,m) printf("%lld %lld\n",n,m);

/******* ALIASING ***********************/

typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ll> vll;
typedef pair<int, int> ii; //pair
typedef vector<ii> vii;   //vector of pair
typedef vector< pair<int, ii> > edgelist; //maintain edge list

/********** USER DEFINED FUNCTIONS *******/

int isprime(int N){
    if(N<2 || (!(N&1) && N!=2))
        return 0;
    for(int i=3; i*i<=N; i+=2){
        if(!(N%i))
            return 0;
    }
    return 1;
}

int power(int a,int b)
{
    int res=1;
    a=a%mod;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%mod;
        b=b>>1;
        a=(a*a)%mod;
    }
    return res;
}

/********** LOGIC STARTS HERE ************/

int main()
{
   string str;
   int n;
   ind(n)
   ll arr[n],sum1=-1,ans1=0,ans2=0,sum2=-1,back[n],front[n];
    memset(back,0,sizeof(back));
    memset(front,0,sizeof(front));
    upto(i,n)
        inlld(arr[i])
        cin>>str;
    if(n!=1)
    {
    upto(i,n)
    {
        if(str[i]=='B')
            front[i]+=arr[i]+front[i-1];
        else
        {   if(i-1>=0)
            front[i]=front[i-1];}
    }
    rev(i,n-1,0)
    {
        if(str[i]=='B')
            back[i]+=arr[i]+back[i+1];
        else
        {if(i+1<n)
            back[i]=back[i+1];}
    }
    upto(i,n)
    {
        if(str[i]=='A')
            ans1+=arr[i];
        if(i+1 < n)
            ans1+=back[i+1];
        sum1=max(sum1,ans1);
        ans1-=back[i+1];
    }

    rev(i,n-1,0)
    {
        if(str[i]=='A')
            ans2+=arr[i];
        if(i-1 >=0)
            ans2+=front[i-1];
        sum2=max(sum2,ans2);
        ans2-=front[i-1];
    }
    oplld(max(sum1,sum2))
    }
    else
    {
        oplld(arr[0])
    }
}


#include<iostream>
using namespace std;
long long mas[500001];
long long n,a,sum,sum1,ans,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      t=sum1;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=sum1;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      if(n == 1)
      cout<<1<<endl;
      else
      cout<<ans<<endl;
}


#include<iostream>
using namespace std;
int mas[500001];
int n,a,sum,sum1,ans,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      t=sum1;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=sum1;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      if(n == 1)
      cout<<1<<endl;
      else
      cout<<ans<<endl;
}


#include<iostream>
using namespace std;
int mas[500001];
int n,a,sum,sum1,ans,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      t=sum1;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=sum1;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      if(n == 1)
      cout<<1<<endl;
      else
      cout<<ans<<endl;

      //system("pause");
}


#include<iostream>
using namespace std;
long long mas[500001];
long long n,a,sum,sum1,ans=1,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      t=sum1;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=sum1;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      cout<<ans<<endl;
}


#include<iostream>
using namespace std;
long long mas[500001];
long long n,a,sum,sum1,ans=1,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      ans=sum1;
      t=sum1;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=sum1;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      cout<<ans<<endl;
}


#include<iostream>
using namespace std;
long long mas[500001];
long long n,a,sum,sum1,ans,t;
string s;
main()
{
      cin>>n;
      for(int i=1;i<=n;i++) {
      cin>>mas[i];
      sum+=mas[i];
      }
      cin>>s;

      for(int i=0;i<n;i++) {
      if(s[i] == 'B')
      sum1+=mas[i+1];
      }

      ans=sum1;
      t=ans;
      for(int i=1;i<=n;i++) {
      if(s[i-1] == 'A')
      t+=mas[i];
      else
      t-=mas[i];
      ans=max(ans,t);
      }

      t=ans;
      for(int j=n;0<j;j--) {
      if(s[j-1] == 'A')
      t+=mas[j];
      else
      t-=mas[j];
      ans=max(ans,t);
      }

      cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define MAX 500000
using namespace std;
int n, p[MAX];
string s;
long long ans, str, tmp;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)  scanf("%d", &p[i]);
    cin >> s;
    for(int i = 0; i < n; i++)
        if(s[i] == 'B') str += p[i];
    ans = tmp = str;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        ans = max(ans, str);
    }
    str = tmp;
    for(int i = n - 1; i >= 0; i--)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        ans = max(ans, str);
    }

    printf("%I64d\n", ans);
}


#include <bits/stdc++.h>
#define MAX 500000
using namespace std;
int n, p[MAX];
string s;
long long ans, str, str2;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)scanf("%d", &p[i]);
    cin >> s;
    for(int i = 0; i < n; i++)
        if(s[i] == 'B') str += p[i];

    ans = str2 = str;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        if(s[n - i - 1] == 'A') str2 += p[n - i - 1];
        else str2 -= p[n - i - 1];
        ans = max(ans, max(str, str2));
    }

    printf("%I64d", ans);
}


#include <iostream>
#define MAX 500005
using namespace std;
int n, p[MAX];
string s;
long long ans, str, str2;

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) cin >> p[i];
    cin >> s;
    for(int i = 0; i < n; i++)
        if(s[i] == 'B') str += p[i];

    ans = str2 = str;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        if(s[n - i - 1] == 'A') str2 += p[n - i - 1];
        else str2 -= p[n - i - 1];
        ans = max(ans, max(str, str2));
    }

    cout << ans;
}


#include <iostream>
#define MAX 500000
using namespace std;
int n, p[MAX];
string s;
long long ans, str, tmp;

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) cin >> p[i];
    cin >> s;
    for(int i = 0; i < n; i++)
        if(s[i] == 'B') str += p[i];
    ans = tmp = str;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        ans = max(ans, str);
    }
    str = tmp;
    for(int i = n - 1; i >= 0; i--)
    {
        if(s[i] == 'A') str = str + p[i];
        else str -= p[i];
        ans = max(ans, str);
    }
    cout << ans;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

struct comand {
	long long int data;
	char ch;
};

int main()
{
	int i, j, n;
	long long int *arr, *summ, suffmax, before = 0, prefmax;
	cin >> n;
	char *initials = (char *)malloc(sizeof(initials) * n);
	arr = (long long int *)calloc(sizeof(arr), n);
	for (i = 0; i < n; i++)
		cin >> arr[i];
	cin >> initials;
	summ = (long long int *)calloc(sizeof(summ), n);
	struct comand **comand = (struct comand**)malloc(sizeof(comand) * n);
	for (i = 0; i < n; i++) {
		comand[i] = (struct comand*)malloc(sizeof(comand[i]));
		comand[i]->data = arr[i];
		comand[i]->ch = initials[i];
		if (initials[i] == 'B')
			before += arr[i];
	}
	summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
	for (suffmax = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > suffmax)
			suffmax = summ[i];
	}
	summ[0] = (comand[0]->ch == 'A' ? comand[0]->data : -(comand[0]->data));
	for (prefmax = summ[0], i = 1, j = 1; i < n; i++, j++) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > prefmax)
			prefmax = summ[i];
	}
	if (prefmax != suffmax)
		prefmax = max(prefmax, suffmax);
	if (prefmax > 0)
		printf("%lld", prefmax + before);
	else
		printf("%lld", before);
	for (i = 0; i < n; i++) {
		free(comand[i]);
	}
	free(comand);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

struct comand {
	long long int data;
	char ch;
};

int main()
{
	int i, j, n;
	long long int *arr, *summ, suffmax, before = 0, prefmax;
	cin >> n;
	char *initials = (char *)malloc(sizeof(initials) * n);
	arr = (long long int *)calloc(sizeof(arr), n);
	for (i = 0; i < n; i++)
		cin >> arr[i];
	cin >> initials;
	summ = (long long int *)calloc(sizeof(summ), n);
	struct comand **comand = (struct comand**)malloc(sizeof(comand) * n);
	for (i = 0; i < n; i++) {
		comand[i] = (struct comand*)malloc(sizeof(comand[i]));
		comand[i]->data = arr[i];
		comand[i]->ch = initials[i];
		if (initials[i] == 'B')
			before += arr[i];
	}
	summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
	for (suffmax = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > suffmax)
			suffmax = summ[i];
	}
	summ[0] = (comand[0]->ch == 'A' ? comand[0]->data : -(comand[0]->data));
	for (prefmax = summ[0], i = 1, j = 1; i < n; i++, j++) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > prefmax)
			prefmax = summ[i];
	}
	if (prefmax != suffmax)
		prefmax = max(prefmax, suffmax);
	if (prefmax > 0)
		printf("%I64d", prefmax + before);
	else
		printf("%I64d", before);
	for (i = 0; i < n; i++) {
		free(comand[i]);
	}
	free(comand);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

struct comand {
    long long int data;
    char ch;
};

int main()
{
    int i, j, n;
    long long int *arr, *summ, suffmax, before = 0, prefmax;
    cin >> n;
    char *initials = new char [500005];
    arr = new long long int [500005];
    for (i = 0; i < n; i++)
	cin >> arr[i];
    cin >> initials;
    summ = new long long int [500005];
    struct comand **comand = new struct comand* [500005];
    for (i = 0; i < n; i++) {
	comand[i] = new struct comand;
	comand[i]->data = arr[i];
	comand[i]->ch = initials[i];
	if (initials[i] == 'B')
	    before += arr[i];
    }
    summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
    for (suffmax = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
	if (comand[j]->ch == 'A')
	    summ[i] = comand[j]->data + summ[i - 1];
	else
	    summ[i] = summ[i - 1] - comand[j]->data;
	if (summ[i] > suffmax)
	    suffmax = summ[i];
    }
    summ[0] = (comand[0]->ch == 'A' ? comand[0]->data : -(comand[0]->data));
    for (prefmax = summ[0], i = 1, j = 1; i < n; i++, j++) {
	if (comand[j]->ch == 'A')
	    summ[i] = comand[j]->data + summ[i - 1];
	else
	    summ[i] = summ[i - 1] - comand[j]->data;
	if (summ[i] > prefmax)
	    prefmax = summ[i];
    }
    if (prefmax != suffmax)
	prefmax = max(prefmax, suffmax);
    if (prefmax > 0)
	printf("%lld", prefmax + before);
    else
	printf("%lld", before);
    for (i = 0; i < n; i++) {
	delete[](comand[i]);
    }
    delete[](comand);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct comand {
	int data;
	char ch;
};

int main()
{
	int i, j, n, *arr, *summ, max, before = 0;
	//const char *initials = "ABBABAABBB";
	cin >> n;
	char *initials = (char *)malloc(sizeof(initials) * n);
	arr = (int *)calloc(sizeof(arr), n);
	for (i = 0; i < n; i++)
		cin >> arr[i];
	cin >> initials;
	summ = (int *)calloc(sizeof(summ), n);
	struct comand **comand = (struct comand**)malloc(sizeof(comand) * n);
	for (i = 0; i < n; i++) {
		comand[i] = (struct comand*)malloc(sizeof(comand[i]));
		comand[i]->data = arr[i];
		comand[i]->ch = initials[i];
		if (initials[i] == 'B')
			before += arr[i];
	}
	//comand = selectSort(comand, n);
	/*for (i = 0; i < n; i++)
		printf("%d ", comand[i]->data);
	for (i = 0; i < n; i++)
		printf("%c ", comand[i]->ch);*/
	summ[0] = (comand[n - 1]->ch == 'A' ? comand[n - 1]->data : -(comand[n - 1]->data));
	for (max = summ[0], i = 1, j = n - 2; i < n; i++, j--) {
		if (comand[j]->ch == 'A')
			summ[i] = comand[j]->data + summ[i - 1];
		else
			summ[i] = summ[i - 1] - comand[j]->data;
		if (summ[i] > max)
			max = summ[i];
	}
	if (max > 0)
		printf("%d", max + before);
	else
		printf("%d", before);
	for (i = 0; i < n; i++) {
		//printf("%d ", summ[i]);
		free(comand[i]);
	}
	free(comand);
	return 0;
}