#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        int t=0;
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;
       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t)
       continue;
       for(i=1;i<=9;i++)
       for(j=1;j<=9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0)
           {
               t=1;
               mp[i]--;mp[j]--;
            for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               t=1;
               mp[i]=mp[i]-2;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
       }

       if(t==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	else
		gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
	long n,i,k,s,x;
	while(scanf("%ld",&n)!=EOF)
	{
		s=0;x=0;
		int f=0;
		int a[100005]={0};
		map<int,int> mp;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]==0)
			f=1;
			  mp[a[i]]++;

		}
		k=0;
        k=MIN(mp[8],mp[7]);
		mp[8]-=k;
		mp[7]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[7],mp[5]);
		mp[7]-=k;
		mp[5]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[8],mp[3]);
		mp[8]-=k;
		mp[3]-=k;

	//	printf("%d\n",k);

		k=MIN(mp[7],mp[2]);
		mp[7]-=k;
		mp[2]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[5],mp[4]);
		mp[5]-=k;
		mp[4]-=k;
		//printf("%d\n",k);
		k=MIN(mp[4],mp[2]);
		mp[4]-=k;
		mp[2]-=k;
    // printf("%d\n",k);
		s=mp[2]*2;
		k=0;
        while(s%3!=0&&s)
		{
		   s=s-2;
		   k++;
		}
		mp[2]=k;
      k=0;
		s=mp[4]*4;
        while(s%3!=0)
		{
		   s=s-4;
		   k++;
		}
		mp[4]=k;
    k=0;
		s=mp[5]*5;
        while(s%3!=0)
		{
		   s=s-5;
		   k++;
		}
		mp[5]=k;
        k=0;
		s=mp[7]*7;
        while(s%3!=0)
		{
		   s=s-7;
		   k++;
		}
		mp[7]=k;
       k=0;
		s=mp[8]*8;
        while(s%3!=0)
		{
		   s=s-8;
		   k++;
		}
		mp[8]=k;
		k=0;
   sort(a,a+n);
   //cout<<mp[5]<<"jj";
   int x=0;
   if(f){
    for(i=n-1;i>=0;i--)
	{
	  if(a[i]%3!=0&&mp[a[i]]>0)
	   {

		  k=mp[a[i]];
		   mp[a[i]]=0;
		//  printf("%ld %d\n",k,a[i]);
		   i-=k-1;
		  // printf("%d\n",i);
		  // x++;
		  // if(x==5) break;
	   }
	  else
	  {
	      f=0;
	      printf("%ld",a[i]);
	  }

	}
	if(f)
	printf("-1");
	printf("\n");
   }
   else printf("-1\n");
	}
	return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;

       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       if(g==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int n,i,x,a[15],j,k;
bool p;
vector<int> ans;
int fix[15],sum;

void rec(int k){
     if(k==9){if(sum!=0 && sum%3==0)
               for(i=1;i<=8;i++){
                if(fix[i]>0)a[i]-=fix[i];
                ans.push_back(i);
                if(fix[i]==2)
                 ans.push_back(i);
                 }
               return;}

    if(a[k]==0){rec(k+1);return;}
    sum+=k;
    fix[k]=1;
    rec(k+1);
    if(a[i]==2){sum+=k;
                 rec(k+1);}
    fix[k]=0;
}

int main()
{cin>>n;
 for(i=1;i<=n;i++){
  cin>>x;
  if(x==0)p=1;
  a[x]++;}
 if(!p){cout<<-1<<endl;return 0;}
 p=1;
 for(i=1;i<=a[0];i++)ans.push_back(0);
 a[0]=0;
 for(i=1;i<=a[9];i++){ans.push_back(9);p=1;}
 a[9]=0;
 for(i=1;i<=a[3];i++){ans.push_back(3);p=1;}
 a[3]=0;
 for(i=1;i<=a[6];i++){ans.push_back(6);p=1;}
 a[6]=0;

 for(i=1;i<=8;i++)
  while(a[i]>2){
    ans.push_back(i);
    ans.push_back(i);
    ans.push_back(i);
    p=1;
    a[i]-=3;}

 rec(1);

 if(!p){cout<<-1<<endl;return 0;}
 sort(ans.begin(),ans.end());
 for(i=ans.size()-1;i>=0;i--)
  cout<<ans[i];
  cout<<endl;
// system("pause");
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int n,i,x,a[15],j,k;
bool p;
vector<int> ans;
int fix[15],sum;

void rec(int k){
     if(k==9){if(sum!=0 && sum%3==0)
               for(int i=1;i<=8;i++){
                if(fix[i]>0){a[i]-=fix[i];
                            ans.push_back(i);}
                if(fix[i]==2)
                 ans.push_back(i);
                 }
               return;}

    if(a[k]==0){rec(k+1);return;}
    if(a[k]==2){sum+=k+k;
                 fix[k]=2;
                 rec(k+1);
                sum-=k+k;

                 }
    if(a[k]==1){
                sum+=k;
               fix[k]=1;
                  rec(k+1);
                  sum-=k;}
    fix[k]=0;
}

int main()
{cin>>n;
 for(i=1;i<=n;i++){
  cin>>x;
  if(x==0)p=1;
  a[x]++;}
 if(!p){cout<<-1<<endl;return 0;}

 for(i=1;i<=a[0];i++)ans.push_back(0);
 a[0]=0;
 for(i=1;i<=a[9];i++)ans.push_back(9);
 a[9]=0;
 for(i=1;i<=a[3];i++)ans.push_back(3);
 a[3]=0;
 for(i=1;i<=a[6];i++)ans.push_back(6);
 a[6]=0;

 for(i=1;i<=8;i++)
  while(a[i]>2){
    ans.push_back(i);
    ans.push_back(i);
    ans.push_back(i);
    a[i]-=3;}

 rec(1);

 sort(ans.begin(),ans.end());
 for(i=ans.size()-1;i>=0;i--)
  cout<<ans[i];
  cout<<endl;
// system("pause");
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int n,i,x,a[15],j,k;
bool p;
vector<int> ans;
int fix[15],sum;

void rec(int k){
     if(k==9){if(sum!=0 && sum%3==0)
               for(int i=1;i<=8;i++){
                if(fix[i]>0){a[i]-=fix[i];
                            ans.push_back(i);}
                if(fix[i]==2)
                 ans.push_back(i);
                 }
               return;}

    if(a[k]==0){rec(k+1);return;}
    if(a[i]==2){sum+=k+k;
                 fix[k]=2;
                 rec(k+1);
                sum-=k+k;

                 }
    if(a[i]==1){
                sum+=k;
               fix[k]=1;
                  rec(k+1);
                  sum-=k;}
    fix[k]=0;
}

int main()
{cin>>n;
 for(i=1;i<=n;i++){
  cin>>x;
  if(x==0)p=1;
  a[x]++;}
 if(!p){cout<<-1<<endl;return 0;}

 for(i=1;i<=a[0];i++)ans.push_back(0);
 a[0]=0;
 for(i=1;i<=a[9];i++)ans.push_back(9);
 a[9]=0;
 for(i=1;i<=a[3];i++)ans.push_back(3);
 a[3]=0;
 for(i=1;i<=a[6];i++)ans.push_back(6);
 a[6]=0;

 for(i=1;i<=8;i++)
  while(a[i]>2){
    ans.push_back(i);
    ans.push_back(i);
    ans.push_back(i);
    a[i]-=3;}

 rec(1);

 sort(ans.begin(),ans.end());
 for(i=ans.size()-1;i>=0;i--)
  cout<<ans[i];
  cout<<endl;
// system("pause");
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int n,i,x,a[15],j,k;
bool p;
vector<int> ans;
int fix[15],sum;

void rec(int k){
     if(k==0){if(sum!=0 && sum%3==0)
               for(int i=1;i<=8;i++){
                if(fix[i]>0){a[i]-=fix[i];
                            ans.push_back(i);p=1;}
                if(fix[i]==2)
                 ans.push_back(i);
                 }
               return;}

    if(a[k]==0){rec(k-1);return;}
    if(a[k]==2){sum+=k+k;
                 fix[k]=2;
                 rec(k-1);
                sum-=2*k;

                 }
    if(a[k]==1){
                sum+=k;
               fix[k]=1;
                  rec(k-1);
                  sum-=k;}
    fix[k]=0;
}

int main()
{cin>>n;
 for(i=1;i<=n;i++){
  cin>>x;
  if(x==0)p=1;
  a[x]++;}
 if(!p){cout<<-1<<endl;return 0;}

 for(i=1;i<=a[0];i++)ans.push_back(0);
 a[0]=0;
 for(i=1;i<=a[9];i++){ans.push_back(9);p=1;}
 a[9]=0;
 for(i=1;i<=a[3];i++){ans.push_back(3);p=1;}
 a[3]=0;
 for(i=1;i<=a[6];i++){ans.push_back(6);p=1;}
 a[6]=0;

 for(i=1;i<=8;i++)
  while(a[i]>2){
    p=1;
    ans.push_back(i);
    ans.push_back(i);
    ans.push_back(i);
    a[i]-=3;}

 rec(8);
 if(!p){cout<<0<<endl;return 0;}
 sort(ans.begin(),ans.end());
 for(i=ans.size()-1;i>=0;i--)
  cout<<ans[i];
  cout<<endl;
// system("pause");
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5e5+10;
const long long inf=-maxn*1e9;
typedef long long ll;
int n,p[maxn];
ll sum[maxn];
char s[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&p[i]);
        scanf("%s",s);
        ll ans=0;
        if(s[0]=='A')
            sum[0]=-p[0];
        else
        {
            sum[0]=p[0];
            ans+=p[0];
        }
        for(int i=1;i<n;i++)
        {
            if(s[i]=='A')
                sum[i]=sum[i-1]-p[i];
            else
            {
                ans+=p[i];
                sum[i]=sum[i-1]+p[i];
            }
        }
        ll maxi=inf;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='B')
            {
                flag=1;
                if(i==0)
                {
                    maxi=max(maxi,sum[i]-sum[n-1]);
                }
                else if(i==n-1)
                {
                    maxi=max(maxi,-sum[i-1]);
                }
                else
                {
                    maxi=max(maxi,max(sum[i]-sum[n-1],-sum[i-1]));
                }
            }
        }
        if(flag)
        {
            if(maxi>0)
                printf("%I64d\n",ans+maxi);
            else
                printf("%I64d\n",ans);
        }
        else
        {
            ans=0;
            for(int i=0;i<n;i++)
                ans+=p[i];
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define SF scanf
#define PF printf
#define PB push_back
#define MP make_pair
#define mx 100005
#define INP          freopen("in.txt", "r", stdin);
#define OUT          freopen("out.txt", "w", stdout);
#define BOOST        std::ios_base::sync_with_stdio(false);
int a[221000];
char b[221000];

int main()
{
    int n,t,i,j,k=0,n1,n2,mx1=0,l,r,m,p,ans=0;

    SF("%d",&n);
    for(i=0;i<n;i++)
    {
        SF("%d",&a[i]);
    }
    SF("%s",b);
    for(i=0;i<n;i++)
    {
        //cout<<b[i]<<endl;
        if(b[i]=='B')
        {
            ans+=a[i];
            mx1+=a[i];
            k+=a[i];
        }
    }
    //cout<<ans<<endl;
    for(i=0;i<n;i++)
    {
        if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    ans=k;
    for(i=n-1;i>=0;i--)
    {
       if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    cout<<mx1<<endl;

return 0;
}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define SF scanf
#define PF printf
#define PB push_back
#define MP make_pair
#define mx 100005
#define INP          freopen("in.txt", "r", stdin);
#define OUT          freopen("out.txt", "w", stdout);
#define BOOST        std::ios_base::sync_with_stdio(false);
int a[621000];
char b[621000];

int main()
{
    ll n,t,i,j,k=0,n1,n2,mx1=0,l,r,m,p,ans=0;

    SF("%lld",&n);
    for(i=0;i<n;i++)
    {
        SF("%lld",&a[i]);
    }
    SF("%s",b);
    for(i=0;i<n;i++)
    {
        //cout<<b[i]<<endl;
        if(b[i]=='B')
        {
            ans+=a[i];
            mx1+=a[i];
            k+=a[i];
        }
    }
    //cout<<ans<<endl;
    for(i=0;i<n;i++)
    {
        if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    ans=k;
    for(i=n-1;i>=0;i--)
    {
       if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    cout<<mx1<<endl;

return 0;
}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define SF scanf
#define PF printf
#define PB push_back
#define MP make_pair
#define mx 100005
#define INP          freopen("in.txt", "r", stdin);
#define OUT          freopen("out.txt", "w", stdout);
#define BOOST        std::ios_base::sync_with_stdio(false);
int a[221000];
char b[221000];

int main()
{
    ll n,t,i,j,k=0,n1,n2,mx1=0,l,r,m,p,ans=0;

    SF("%lld",&n);
    for(i=0;i<n;i++)
    {
        SF("%lld",&a[i]);
    }
    SF("%s",b);
    for(i=0;i<n;i++)
    {
        //cout<<b[i]<<endl;
        if(b[i]=='B')
        {
            ans+=a[i];
            mx1+=a[i];
            k+=a[i];
        }
    }
    //cout<<ans<<endl;
    for(i=0;i<n;i++)
    {
        if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    ans=k;
    for(i=n-1;i>=0;i--)
    {
       if(b[i]=='B')
        {
            ans-=a[i];
        }
        else
        {
            ans+=a[i];
        }
        mx1=max(mx1,ans);
    }
    cout<<mx1<<endl;

return 0;
}


#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;

typedef long long ll;
ll a[600000],b[600000],p[600000];
int main(){
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> p[i];
	}
	string s;
	cin >> s;
	s = " " + s;
	ll maxa = 0,maxb = 0,posa = -1,posb = -1;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A') a[i] = a[i - 1] + p[i];
		else a[i] = a[i - 1] - p[i];
		if (a[i] > maxa)
		{
		//	cout << i << " " << a[i] << endl;
			maxa = a[i];
			posa = i;
		}
	}
	//cout << maxa << endl;
	for (int i = n; i > 0; i--)
	{
		if (s[i] == 'A') b[i] = b[i + 1] + p[i];
		else b[i] = b[i + 1] - p[i];
	//	cout << b[i] << endl;
		if (b[i] > maxb)
		{
			maxb = b[i];
			posb = i;
		}
	}
//	cout << posa << " " << posb << endl;
	ll ans = 0;
	if (posa == -1 && posb == -1)
	{
		for (int i = 1; i <= n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else if (maxa >= maxb)
	{
		for (int i = 1; i <= posa; i++)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posa+1; i <= n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else
	{
	//	cout <<"....." << endl;
		for (int i = n; i >= posb; i--)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posb - 1; i > 0; i--)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;

typedef long long ll;
ll a[60000],b[60000],p[60000];
int main(){
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> p[i];
	}
	string s;
	cin >> s;
	int maxa = 0,maxb = 0,posa = -1,posb = -1;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'A') a[i] = a[i - 1] + p[i];
		else a[i] = a[i - 1] - p[i];
		if (a[i] > maxa)
		{
		//	cout << i << " " << a[i] << endl;
			maxa = a[i];
			posa = i;
		}
	}
	//cout << maxa << endl;
	for (int i = n - 1; i >= 0; i--)
	{
		if (s[i] == 'A') b[i] = b[i - 1] + p[i];
		else b[i] = b[i - 1] - p[i];
		if (b[i] > maxb)
		{
			maxb = b[i];
			posb = i;
		}
	}
//	cout << posa << " " << posb << endl;
	int ans = 0;
	if (posa == -1 && posb == -1)
	{
		for (int i = 0; i < n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else if (maxa >= maxb)
	{
		for (int i = 0; i <= posa; i++)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posa+1; i < n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else
	{
	//	cout <<"....." << endl;
		for (int i = n - 1; i >= posb; i--)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posb - 1; i >= 0; i--)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;

typedef long long ll;
ll a[60000],b[60000],p[60000];
int main(){
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> p[i];
	}
	string s;
	cin >> s;
	s = " " + s;
	ll maxa = 0,maxb = 0,posa = -1,posb = -1;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A') a[i] = a[i - 1] + p[i];
		else a[i] = a[i - 1] - p[i];
		if (a[i] > maxa)
		{
		//	cout << i << " " << a[i] << endl;
			maxa = a[i];
			posa = i;
		}
	}
	//cout << maxa << endl;
	for (int i = n; i > 0; i--)
	{
		if (s[i] == 'A') b[i] = b[i + 1] + p[i];
		else b[i] = b[i + 1] - p[i];
	//	cout << b[i] << endl;
		if (b[i] > maxb)
		{
			maxb = b[i];
			posb = i;
		}
	}
//	cout << posa << " " << posb << endl;
	ll ans = 0;
	if (posa == -1 && posb == -1)
	{
		for (int i = 1; i <= n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else if (maxa >= maxb)
	{
		for (int i = 1; i <= posa; i++)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posa+1; i <= n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else
	{
	//	cout <<"....." << endl;
		for (int i = n; i >= posb; i--)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posb - 1; i > 0; i--)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;

typedef long long ll;
ll a[60000],b[60000],p[60000];
int main(){
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> p[i];
	}
	string s;
	cin >> s;
	ll maxa = 0,maxb = 0,posa = -1,posb = -1;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'A') a[i] = a[i - 1] + p[i];
		else a[i] = a[i - 1] - p[i];
		if (a[i] > maxa)
		{
		//	cout << i << " " << a[i] << endl;
			maxa = a[i];
			posa = i;
		}
	}
	//cout << maxa << endl;
	for (int i = n - 1; i >= 0; i--)
	{
		if (s[i] == 'A') b[i] = b[i - 1] + p[i];
		else b[i] = b[i - 1] - p[i];
		if (b[i] > maxb)
		{
			maxb = b[i];
			posb = i;
		}
	}
//	cout << posa << " " << posb << endl;
	ll ans = 0;
	if (posa == -1 && posb == -1)
	{
		for (int i = 0; i < n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else if (maxa >= maxb)
	{
		for (int i = 0; i <= posa; i++)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posa+1; i < n; i++)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	else
	{
	//	cout <<"....." << endl;
		for (int i = n - 1; i >= posb; i--)
		{
			if (s[i] == 'A') ans += p[i];
		}
		for (int i = posb - 1; i >= 0; i--)
		{
			if (s[i] == 'B') ans += p[i];
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define in(n) scanf("%I64d",&n)
#define MOD 1000000007
#define MAXN 500005
ll s[MAXN];
ll pa[MAXN];
ll pb[MAXN];
ll sa[MAXN];
ll sb[MAXN];

int main()
{
    ll n,i,d,maxi;
    string st;
    in(n);
    for(i=0;i<n;i++)
        {
            in(s[i]);
        }
    cin>>st;
    pa[0]=0;
    pb[0]=0;
    if(st[0]=='A')
        pa[0]=s[0];
    else
        pb[0]=s[0];

    for(i=1;i<n;i++)
        {
            pa[i]=pa[i-1];
            pb[i]=pb[i-1];
            if(st[i]=='A')
                pa[i]+=s[i];
            else
                pb[i]+=s[i];
        }
    sa[n]=0;
    sb[n]=0;
    for(i=n-1;i>=0;i--)
        {
            sa[i]=sa[i+1];
            sb[i]=sb[i+1];
            if(st[i]=='A')
                sa[i]+=s[i];
            else
                sb[i]+=s[i];
        }

    maxi=max(pb[n-1],sb[0]);
    for(i=1;i<n;i++)
        {
            d = sa[i]-sb[i] + pb[i-1];
            if(d>maxi)
            maxi=d;
        }

    for(i=0;i<n;i++)
        {
            d = pa[i]-pb[i] + sb[i+1];
            if(d>maxi)
            maxi=d;
        }
    cout<<maxi<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define MAXN 500005
ll s[MAXN];
ll pa[MAXN];
ll pb[MAXN];
ll sa[MAXN];
ll sb[MAXN];

int main()
{
    ll n,i,d,maxi;
    string st;
    cin>>n;
    for(i=0;i<n;i++)
        {
        cin>>s[i];
        }
    cin>>st;
    pa[0]=0;
    pb[0]=0;
    if(st[0]=='A')
        pa[0]=s[0];
    else
        pb[0]=s[0];

    for(i=1;i<n;i++)
        {
            pa[i]=pa[i-1];
            pb[i]=pb[i-1];
            if(st[i]=='A')
                pa[i]+=s[i];
            else
                pb[i]+=s[i];
        }
    sa[n]=0;
    sb[n]=0;
    for(i=n-1;i>=0;i--)
        {
            sa[i]=sa[i+1];
            sb[i]=sb[i+1];
            if(st[i]=='A')
                sa[i]+=s[i];
            else
                sb[i]+=s[i];
        }

    maxi=max(pb[n-1],sb[0]);
    for(i=0;i<n;i++)
        {
            d = sa[i+1]-sb[i+1] + pb[i];
            if(d>maxi)
            maxi=d;
        }

    for(i=0;i<n;i++)
        {
            d = pa[i]-pb[i] + sb[i+1];
            if(d>maxi)
            maxi=d;
        }
    cout<<maxi<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define MAXN 500005
ll s[MAXN];
ll pa[MAXN];
ll pb[MAXN];
ll sa[MAXN];
ll sb[MAXN];

int main()
{
    ll n,i,d,maxi;
    string st;
    cin>>n;
    for(i=0;i<n;i++)
        {
        cin>>s[i];
        }
    cin>>st;
    pa[0]=0;
    pb[0]=0;
    if(st[0]=='A')
        pa[0]=s[0];
    else
        pb[0]=s[0];

    for(i=1;i<n;i++)
        {
            pa[i]=pa[i-1];
            pb[i]=pb[i-1];
            if(st[i]=='A')
                pa[i]+=s[i];
            else
                pb[i]+=s[i];
        }
    sa[n]=0;
    sb[n]=0;
    for(i=n-1;i>=0;i--)
        {
            sa[i]=sa[i+1];
            sb[i]=sb[i+1];
            if(st[i]=='A')
                sa[i]+=s[i];
            else
                sb[i]+=s[i];
        }

    maxi=max(pb[n-1],sb[0]);
    for(i=0;i<n;i++)
        {
            d = sa[i+1] + pb[i];
            if(d>maxi)
            maxi=d;
        }

    for(i=0;i<n;i++)
        {
            d = pa[i] + sb[i+1];
            if(d>maxi)
            maxi=d;
        }
    cout<<maxi<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FILE ""

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    //
    int n;
    char c = getchar();
    while (c < '0' || c > '9'){
        c = getchar();
    }
    n = 0;
    while (c >= '0' && c <= '9'){
        n *= 10;
        n += (int)(c - '0');
        c = getchar();
    }
    int a[n];
    for (int i = 0; i < n; i++){
        char c = getchar();
        while (c < '0' || c > '9'){
            c = getchar();
        }
        a[i] = 0;
        while (c >= '0' && c <= '9'){
            a[i] *= 10;
            a[i] += (int)(c - '0');
            c = getchar();
        }
    }
    c = getchar();
    while (c != 'A' && c != 'B'){
        c = getchar();
    }
    string s;
    while (c == 'A' || c == 'B'){
        s += c;
        c = getchar();
    }
    long long sb = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'B'){
            sb += a[i];
        }
    }
    long long cur1 = sb;
    long long cur2 = sb;
    long long ans1 = sb;
    long long ans2 = sb;
    for (int i = 0; i < n; i++){
        if (s[i] == 'A'){
            cur1 += a[i];
        }
        else{
            cur1 -= a[i];
        }
        if (cur1 > ans1) ans1 = cur1;
        if (s[n - i - 1] == 'A'){
            cur2 += a[n - i - 1];
        }
        else{
            cur2 -= a[n - i - 1];
        }
        if (cur2 > ans2) ans2 = cur2;
    }

    if (ans2 > ans1) ans1 = ans2;
    printf("%I64d", ans1);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FILE ""

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    //
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    char c = getchar();
    while (c != 'A' && c != 'B'){
        c = getchar();
    }
    string s;
    while (c == 'A' || c == 'B'){
        s += c;
        c = getchar();
    }
    long long sb = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'B'){
            sb += a[i];
        }
    }
    long long cur = sb;
    long long ans1 = sb;
    long long ans2 = sb;
    for (int i = 0; i < n; i++){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans1 = max(ans1 , cur);
    }
    cur = sb;
    for (int i = n - 1; i >= 0; i--){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans2 = max(ans2 , cur);
    }

    cout << max(ans1 , ans2);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FILE ""

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    //
    int n;
    char c = getchar();
    while (c < '0' || c > '9'){
        c = getchar();
    }
    n = 0;
    while (c >= '0' && c <= '9'){
        n *= 10;
        n += (int)(c - '0');
        c = getchar();
    }
    int a[n];
    for (int i = 0; i < n; i++){
        char c = getchar();
        while (c < '0' || c > '9'){
            c = getchar();
        }
        a[i] = 0;
        while (c >= '0' && c <= '9'){
            a[i] *= 10;
            a[i] += (int)(c - '0');
            c = getchar();
        }
    }
    c = getchar();
    while (c != 'A' && c != 'B'){
        c = getchar();
    }
    string s;
    while (c == 'A' || c == 'B'){
        s += c;
        c = getchar();
    }
    long long sb = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'B'){
            sb += a[i];
        }
    }
    long long cur1 = sb;
    long long cur2 = sb;
    long long ans1 = sb;
    long long ans2 = sb;
    for (int i = 0; i < n; i++){
        if (s[i] == 'A'){
            cur1 += a[i];
        }
        else{
            cur1 -= a[i];
        }
        ans1 = max(ans1 , cur1);
        if (s[n - i - 1] == 'A'){
            cur2 += a[n - i - 1];
        }
        else{
            cur2 -= a[n - i - 1];
        }
        ans2 = max(ans2 , cur2);
    }

    cout << max(ans1 , ans2);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FILE ""

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    //
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++){
        char c = getchar();
        while (c < '0' || c > '9'){
            c = getchar();
        }
        a[i] = 0;
        while (c >= '0' && c <= '9'){
            a[i] *= 10;
            a[i] += (int)(c - '0');
            c = getchar();
        }
    }
    char c = getchar();
    while (c != 'A' && c != 'B'){
        c = getchar();
    }
    string s;
    while (c == 'A' || c == 'B'){
        s += c;
        c = getchar();
    }
    long long sb = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'B'){
            sb += a[i];
        }
    }
    long long cur = sb;
    long long ans1 = sb;
    long long ans2 = sb;
    for (int i = 0; i < n; i++){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans1 = max(ans1 , cur);
    }
    cur = sb;
    for (int i = n - 1; i >= 0; i--){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans2 = max(ans2 , cur);
    }

    cout << max(ans1 , ans2);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FILE ""

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    //
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    char c = getchar();
    while (c != 'A' && c != 'B'){
        c = getchar();
    }
    string s;
    while (c == 'A' || c == 'B'){
        s += c;
        c = getchar();
    }
    long long sb = 0;
    for (int i = 0; i < n; i++){
        if (s[i] == 'B'){
            sb += a[i];
        }
    }
    long long cur = sb;
    long long ans1 = sb;
    long long ans2 = sb;
    for (int i = 0; i < n; i++){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans1 = max(ans1 , cur);
    }
    cur = sb;
    for (int i = n - 1; i >= 0; i--){
        if (s[i] == 'A'){
            cur += a[i];
        }
        else{
            cur -= a[i];
        }
        ans2 = max(ans2 , cur);
    }

    printf("%d\n", max(ans1 , ans2));
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
typedef long double ld ;
typedef long long ll ;
const int PI = acos(-1.0) ;
using namespace std ;
const int Max = 10055 ;
int pra[500005] , prb[500005] ,saa[500005] , sab[500005] ;
int main()
{
    long long n , mx = -1 ;
    string s ;
    cin >> n  ;
    int a[n] ;
    for ( int i = 0 ; i < n ; i++ )
        cin >> a[i] ;
        cin >> s ;
        long long sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < n ; i++ )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2 += a[i] ;
            pra[i] = sum1 ;
            prb[i] = sum2 ;
        }
   mx = max ( sum1 , sum2 ) ;
     sum1 = 0 , sum2 = 0 ;
  for ( int i = n - 1 ; i >= 0 ; i-- )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2+=a[i] ;
                sab[i] = sum2 ;
                saa[i] = sum1 ;
        }
      //mx = max ( max ( sab , saa ) , mx ) ;
      for ( int i = 0 ; i < n ; i++ )
        mx = max ( max ( pra[i] + sab[i+1] , prb[i] + saa[i+1]) , mx ) ;
    cout << mx ;
  return 0 ;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
typedef long double ld ;
typedef long long ll ;
const int PI = acos(-1.0) ;
using namespace std ;
const int Max = 10055 ;
int pra[500005] , prb[500005] ,saa[500005] , sab[500005] ;
int main()
{
    int n , mx = -1 ;
    string s ;
    cin >> n  ;
    int a[n] ;
    for ( int i = 0 ; i < n ; i++ )
        cin >> a[i] ;
        cin >> s ;
        int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < n ; i++ )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2 += a[i] ;
            pra[i] = sum1 ;
            prb[i] = sum2 ;
        }
   mx = max ( sum1 , sum2 ) ;
     sum1 = 0 , sum2 = 0 ;
  for ( int i = n - 1 ; i >= 0 ; i-- )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2+=a[i] ;
                sab[i] = sum2 ;
                saa[i] = sum1 ;
        }
      //mx = max ( max ( sab , saa ) , mx ) ;
      for ( int i = 0 ; i < n ; i++ )
        mx = max ( max ( pra[i] + sab[i+1] , prb[i] + saa[i+1]) , mx ) ;
    cout << mx ;
  return 0 ;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
typedef long double ld ;
typedef long long ll ;
const int PI = acos(-1.0) ;
using namespace std ;
const int Max = 10055 ;
ll pra[500005] , prb[500005] ,saa[500005] , sab[500005] ;
int main()
{
    int n ;
    ll mx = -1 ;
    string s ;
    cin >> n  ;
    int a[n] ;
    for ( int i = 0 ; i < n ; i++ )
        cin >> a[i] ;
        cin >> s ;
        ll sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < n ; i++ )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2 += a[i] ;
            pra[i] = sum1 ;
            prb[i] = sum2 ;
        }
   mx = max ( sum1 , sum2 ) ;
     sum1 = 0 , sum2 = 0 ;
  for ( int i = n - 1 ; i >= 0 ; i-- )
        {
            if ( s[i] == 'A' )
                sum1 += a[i] ;
            else
                sum2+=a[i] ;
                sab[i] = sum2 ;
                saa[i] = sum1 ;
        }
      //mx = max ( max ( sab , saa ) , mx ) ;
      for ( int i = 0 ; i < n ; i++ )
        mx = max ( max ( pra[i] + sab[i+1] , prb[i] + saa[i+1]) , mx ) ;
    cout << mx ;
  return 0 ;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0,toB=0;
	for (i=0;i<n;i++) cin>>p[i];
	string A;
	cin>>A;
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else
		{
			sum-=p[i];
			toB+=p[i];
		}
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	cout<<maxm+toB<<endl;
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long long p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) scanf("%lld",&p[i]);
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	cout<<maxm<<endl;
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) cin>>p[i]
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	cout<<maxm;
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
long long p[520000];
char A[520000];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	long  long sum1=0,sum2=0,maxm=0,toB=0;
	for (i=0;i<n;i++)
		scanf("%I64d",&p[i]);
	scanf("%s",A);
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A')
			sum1+=p[i];
		else
		{
			sum1-=p[i];
			toB+=p[i];
		}
		maxm=max(sum1,maxm);
		if (A[j]=='A')
			sum2+=p[j];
		else
			sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	printf("%I64d",maxm+toB);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j;
	scanf("%d",&n);
	long  long  p[52000],sum1=0,sum2=0,maxm=0,toB=0;
	char A[52000];
	for (i=0;i<n;i++)
		scanf("%I64d",&p[i]);
	scanf("%s",A);
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A')
			sum1+=p[i];
		else
		{
			sum1-=p[i];
			toB+=p[i];
		}
		maxm=max(sum1,maxm);
		if (A[j]=='A')
			sum2+=p[j];
		else
			sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	printf("%I64d",maxm+toB);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) cin>>p[i];
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	printf("%ld",maxm);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0,toB=0;
	for (i=0;i<n;i++) scanf("%lld",&p[i]);
	char A[52000];
	scanf("%s",A);
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else
		{
			sum-=p[i];
			toB+=p[i];
		}
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	printf("%lld",maxm+toB);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long int  p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) scanf("%ld",&p[i]);
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	printf("%ld",maxm);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0,toB=0;
	for (i=0;i<n;i++) scanf("%lld",&p[i]);
	char A[52000];
	scanf("%s",A);
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else
		{
			sum-=p[i];
			toB+=p[i];
		}
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	printf("%lld",maxm);
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long long p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) cin>>p[i];
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	cout<<maxm<<endl;
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j,maxi,a=-1;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0;
	for (i=0;i<n;i++) cin>>p[i];
	string A;
	cin>>A;
	for (i=0;i<n;i++)
	{
		if (A[i]=='B') sum+=p[i];
		maxm=sum2=sum;
	}
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else sum-=p[i];
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
		if (maxm==sum)
		{
			maxi=i;
			a=0;
		}
	else if (maxm==sum2)
		{
			maxi=j;
			a=1;
		}
	}
	cout<<maxm<<endl;
	return 0;
}


#include<iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	long  long  p[52000],sum=0,sum2=0,maxm=0,toB=0;
	for (i=0;i<n;i++) scanf("%lld",&p[i]);
	string A;
	cin>>A;
	for (i=0,j=n-1;i<n;i++,j--)
	{
		if (A[i]=='A') sum+=p[i];
		else
		{
			sum-=p[i];
			toB+=p[i];
		}
		maxm=max(sum,maxm);
		if (A[j]=='A') sum2+=p[j];
		else sum2-=p[j];
		maxm=max(sum2,maxm);
	}
	cout<<maxm+toB<<endl;
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
while(n--) {
sc(a[k]);
maxx=max(a[k],maxx);k++;}

string s;
cin>>s;
string h=s;
int l=s.length();
vector< string >v;
for(int u=l-1;u>=0;u--){
	if(s[u]=='A') s[u]='B';
	else{
		s[u]='A';
	}
	v.push_back(s);

}
for(int u=0;u<=l-1;u++){
	if(h[u]=='A') h[u]='B';
	else{
		h[u]='A';
	}
	v.push_back(h);

}
int sz=v.size();

for(int u=0;u<v.size();u++){
	maxx=max(maxx,cal(v[u]));

}
p(maxx);

	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
vector< string >v;
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
int maxxx=-1;
while(n--) {
sc(a[k++]);
//k++;
//maxxx=max(maxxx,a[k]);k++;
}

//maxx=max(a[k],maxx);k++;}

string s;
cin>>s;
int ansa=0;
string h=s;
int l=s.length();
int ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
maxxx=ansb;
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}
int ansbb=0;
int ansab=0;
for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxx=max(maxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxx=max(maxx,ansbb);
}

}

p(max(maxx,maxxx));

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%lld",&a)
#define     p(a)  printf("%lld\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
ll a[5*100001];
string s;
string h;
vector< string >v;
ll cal(string s){
	int sz=s.size();
	ll sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
ll maxx=-1;
ll maxxx=-1;
ll maxxxx=-1;
while(n--) {
sc(a[k++]);
//k++;
//maxxx=max(maxxx,a[k]);k++;
}

//maxx=max(a[k],maxx);k++;}

cin>>s;
ll ansa=0;
 h=s;
int l=s.length();
ll ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
maxxx=ansb;
ll ansbb=ansb;
ll ansab=ansa;
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}

for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxxxx=max(maxxxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxxxx=max(maxxxx,ansbb);
}

}

p(max(maxxxx,max(maxx,maxxx)));

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
string s;
string h;
vector< string >v;
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
int maxxx=-1;
int maxxxx=-1;
while(n--) {
sc(a[k++]);
//k++;
//maxxx=max(maxxx,a[k]);k++;
}

//maxx=max(a[k],maxx);k++;}

cin>>s;
int ansa=0;
 h=s;
int l=s.length();
int ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
maxxx=ansb;
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}
int ansbb=0;
int ansab=0;
for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxxxx=max(maxxxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxxxx=max(maxxxx,ansbb);
}

}

p(max(maxxxx,max(maxx,maxxx)));

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
string s;
string h;
vector< string >v;
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
int maxxx=-1;
int maxxxx=-1;
while(n--) {
sc(a[k++]);
//k++;
//maxxx=max(maxxx,a[k]);k++;
}

//maxx=max(a[k],maxx);k++;}

cin>>s;
int ansa=0;
 h=s;
int l=s.length();
int ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
maxxx=ansb;
int ansbb=ansb;
int ansab=ansa;
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}

for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxxxx=max(maxxxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxxxx=max(maxxxx,ansbb);
}

}

p(max(maxxxx,max(maxx,maxxx)));

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
while(n--) {
sc(a[k]);
maxx=max(a[k],maxx);k++;}

string s;
cin>>s;
string h=s;
int l=s.length();
vector< string >v;
for(int u=l-1;u>=1;u--){
	if(s[u]=='A') s[u]='B';
	else{
		s[u]='A';
	}
	v.push_back(s);

}
for(int u=0;u<=l-2;u++){
	if(h[u]=='A') h[u]='B';
	else{
		h[u]='A';
	}
	v.push_back(h);

}
int sz=v.size();

for(int u=0;u<v.size();u++){
	maxx=max(maxx,cal(v[u]));

}
p(maxx);

	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
vector< string >v;
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
while(n--) {
sc(a[k++]);}

//maxx=max(a[k],maxx);k++;}

string s;
cin>>s;
int ansa=0;
string h=s;
int l=s.length();
int ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}
int ansbb=0;
int ansab=0;
for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxx=max(maxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxx=max(maxx,ansbb);
}

}

p(maxx);

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
while(n--) sc(a[k++]);
string s;
cin>>s;
string h=s;
int l=s.length();
vector< string >v;
for(int u=l-1;u>=0;u--){
	if(s[u]=='A') s[u]='B';
	else{
		s[u]='A';
	}
	v.push_back(s);

}
for(int u=0;u<=l-1;u++){
	if(h[u]=='A') h[u]='B';
	else{
		h[u]='A';
	}
	v.push_back(h);

}
int sz=v.size();
int maxx=-1;
for(int u=0;u<v.size();u++){
	maxx=max(maxx,cal(v[u]));

}
p(maxx);

	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%d",&a)
#define     p(a)  printf("%d\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

/*
playing with segment trees

*/
int a[5*100001];
vector< string >v;
int cal(string s){
	int sz=s.size();
	int sum=0;
	for(int u=0;u<sz;u++){
		if(s[u]=='B'){
			sum+=a[u];
		}
	}
	return sum;
}

int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
int maxx=-1;
while(n--) {
sc(a[k]);
maxx=max(a[k],maxx);k++;}

string s;
cin>>s;
string h=s;
int l=s.length();

for(int u=l-1;u>=0;u--){
	if(s[u]=='A') s[u]='B';
	else{
		s[u]='A';
	}
	v.push_back(s);

}
for(int u=0;u<=l-1;u++){
	if(h[u]=='A') h[u]='B';
	else{
		h[u]='A';
	}
	v.push_back(h);

}
int sz=v.size();

for(int u=0;u<v.size();u++){
	maxx=max(maxx,cal(v[u]));

}
p(maxx);

v.clear();
	return 0;
}


/*
RAJAT SINGH
VIT VELLORE
*/

#include<bits/stdc++.h>
using namespace std;
#define ll   long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register  int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}

//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define     sc(a)  scanf("%lld",&a)
#define     p(a)  printf("%lld\n",a)
#define     l(i,a)  for(int i=0;i<a;i++)
#define     re(j,a,b)  for(int j=a;j<=b;j++)
# define ll   long long
#define modu 1000000007
ll  modular_pow(ll int  base, ll int  exponent)
{
    if(exponent==0)
    return 1;
    else if(exponent%2==0){
        ll int res=modular_pow(base,exponent/2);
        return (res*res );

    }
    else{
        return (modular_pow(base,exponent-1) * base);

    }
}

 int gcd(int a,int b){
    if(b==0)
    return a;//gcd
    else return gcd(b,a%b);
 }

bool chek(int x){
    return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
    return __builtin_popcount(n);//count number od set bits in a number

}

/*
c++ does my work!!! but some delay

*/

ll a[5*100001];
string s;
string h;
vector< string >v;
/*
easy problem!!!!! but bad debugging by me...
read problem carefully

*/
int main(){

//freopen("input.txt","r",stdin);
int n;
cin>>n;
int k=0;
ll maxx=-1;
ll maxxx=-1;
ll maxxxx=-1;
while(n--) {
sc(a[k++]);

}
cin>>s;
ll ansa=0;
 h=s;
int l=s.length();
ll ansb=0;
for(int u=0;u<l;u++){
	if(s[u]=='B') ansb+=a[u];
	else ansa+=a[u];
}
maxxx=ansb;
ll ansbb=ansb;
ll ansab=ansa;
for(int u=l-1;u>=0;u--){
if(s[u]=='B'){
	ansb-=a[u];
	ansa+=a[u];
	maxx=max(maxx,ansb);
}
else{
	ansb+=a[u];
	ansa-=a[u];
	maxx=max(maxx,ansb);
}
}

for(int u=0;u<=l-1;u++){
if(h[u]=='B'){
	ansbb-=a[u];
	ansab+=a[u];
	maxxxx=max(maxxxx,ansbb);
}
else{
	ansbb+=a[u];
	ansab-=a[u];
	maxxxx=max(maxxxx,ansbb);
}

}

p(max(maxxxx,max(maxx,maxxx)));

v.clear();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int p[n];
   for(int i=0;i<n;i++)
    cin>>p[i];
   string a;
   cin>>a;
   long long m1=0,ind1=0,ind2=a.size()-1,m2=0,sum=0;
   for(int i=0;i<a.length();i++)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m1)
        m1=sum,ind1=i;

   }
   sum=0;
   for(int i=0;i<=ind1;i++)
   {
       if(a.at(i)=='A') sum+=p[i];
   }
   for(int i=ind1+1*(m1!=0);i<a.length();i++)
   {
       if(a.at(i)=='B')
        sum+=p[i];
   }
   m1=sum;
     for(int i=a.length()-1;i>=0;i--)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m2)
        m2=sum,ind2=i;

   }
   sum=0;
   for(int i=0;i<ind2;i++)
   {
       if(a.at(i)=='B') sum+=p[i];
   }
   for(int i=ind2;i<a.length();i++)
   {
       if(a.at(i)=='A')
        sum+=p[i];
   }
   m2=sum;
   cout<<max(m1,m2);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int p[n];
   for(int i=0;i<n;i++)
    cin>>p[i];
   string a;
   cin>>a;
   int m1=0,ind1=0,ind2=a.size()-1,m2=0,sum=0;
   for(int i=0;i<a.length();i++)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m1)
        m1=sum,ind1=i;

   }
   for(int i=ind1+1*(m1!=0);i<a.length();i++)
   {
       if(a.at(i)=='B')
        m1+=p[i];
   }
   sum=0;
     for(int i=a.length()-1;i>=0;i--)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m2)
        m2=sum,ind2=i;

   }
   for(int i=0;i<ind2+1*(m2==0);i++)
   {
       if(a.at(i)=='B')
        m2+=p[i];
   }
   cout<<max(m1,m2);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int p[n];
   for(int i=0;i<n;i++)
    cin>>p[i];
   string a;
   cin>>a;
   unsigned long long m1=0,ind1=0,ind2=a.size()-1,m2=0,sum=0;
   for(int i=0;i<a.length();i++)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m1)
        m1=sum,ind1=i;

   }
   sum=0;
   for(int i=0;i<=ind1;i++)
   {
       if(a.at(i)=='A') sum+=p[i];
   }
   for(int i=ind1+1*(m1!=0);i<a.length();i++)
   {
       if(a.at(i)=='B')
        sum+=p[i];
   }
   m1=sum;
     for(int i=a.length()-1;i>=0;i--)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m2)
        m2=sum,ind2=i;

   }
   sum=0;
   for(int i=0;i<ind2;i++)
   {
       if(a.at(i)=='B') sum+=p[i];
   }
   for(int i=ind2;i<a.length();i++)
   {
       if(a.at(i)=='A')
        sum+=p[i];
   }
   m2=sum;
   cout<<max(m1,m2);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int p[n];
   for(int i=0;i<n;i++)
    cin>>p[i];
   string a;
   cin>>a;
   int m1=0,ind1=0,ind2=a.size()-1,m2=0,sum=0;
   for(int i=0;i<a.length();i++)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m1)
        m1=sum,ind1=i;

   }
   sum=0;
   for(int i=0;i<=ind1;i++)
   {
       if(a.at(i)=='A') sum+=p[i];
   }
   for(int i=ind1+1*(m1!=0);i<a.length();i++)
   {
       if(a.at(i)=='B')
        sum+=p[i];
   }
   m1=sum;
     for(int i=a.length()-1;i>=0;i--)
   {

       if(a.at(i)=='A')
       {
           sum+=p[i];

       }
       if(a.at(i)=='B')
       {
           sum-=p[i];
       }
       if(sum>m2)
        m2=sum,ind2=i;

   }
   sum=0;
   for(int i=0;i<ind2;i++)
   {
       if(a.at(i)=='B') sum+=p[i];
   }
   for(int i=ind2;i<a.length();i++)
   {
       if(a.at(i)=='A')
        sum+=p[i];
   }
   m2=sum;
   cout<<max(m1,m2);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[500010];
long long A[500010],B[500010];
int main() {
    char s[500010];
    int n;
    long long ans;

    while(~scanf("%d",&n)) {
        for(int i = 1; i <= n; i ++) {
            scanf("%d",&a[i]);
        }
        scanf("%s",s + 1);
        A[0] = B[0] = 0;
        for(int i = 1; i <= n; i ++) {
            A[i] = A[i - 1];
            B[i] = B[i - 1];
            if(s[i] == 'A') A[i] += a[i];
            else B[i] += a[i];
        }
        for(int i = 0;i <= n + 1; i ++) {
            ans = max(ans, A[i] + B[n] - B[i]);
            ans = max(ans, B[i] + A[n] - A[i]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[500010];
int main() {
    char s[500010];
    int n;
    long long ans;
    while(~scanf("%d",&n)) {
        ans = 0;
        for(int i = 0; i < n; i ++) {
            scanf("%d",&a[i]);
        }
        scanf("%s",s);
        for(int i = 0; i < n; i ++) {
            if(s[i] == 'B')
                ans += a[i];
        }
        //printf("%d\n",ans);
        int j = 0;
        long long maxx = 0,k = 0;
        int i = 0;
        while(1) {
            if(s[i] != 'A') break;
            maxx += a[i];
            i ++;
        }
        i = n - 1;
        while(1) {
            if(s[i] != 'A')break;
            k += a[i];
            i --;
        }
        maxx = max(maxx, k);
        printf("%lld\n",maxx + ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[500010];
int main() {
    char s[500010];
    int n;
    long long ans;
    while(~scanf("%d",&n)) {
        ans = 0;
        for(int i = 0; i < n; i ++) {
            scanf("%d",&a[i]);
        }
        scanf("%s",s);
        for(int i = 0; i < n; i ++) {
            if(s[i] == 'B')
                ans += a[i];
        }
        //printf("%d\n",ans);
        int j = 0;
        long long maxx = 0;
        for(int i = 0; i < n; i ++) {
            long long k = 0;
            while(1) {
                if(s[i + j] != 'A') break;

                k += a[i + j];j ++;
            }
            j = 0;
            maxx = max(maxx, k);
            //printf("%d\n",maxx);
        }
        printf("%lld\n",maxx + ans);
    }
    return 0;
}


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

long long p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	string s ;
	int ans = 0 ;
	vector < int > v ;
	cin >> s ;
	for ( int i = 0 ; i < s.size() ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	reverse ( s.begin() , s.end() ) ;
	ans = v[0] ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[n-i-1] ;
		else ans -= p[n-i-1] ;
		v.push_back ( ans ) ;
	}
	sort ( v.begin() , v.end() ) ;
	cout << v.back () << endl ;
	return 0 ;
}

// The end !!!


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

long long p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	string s;
	vector < long long > v ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	cin >> s ;
	long long ans = 0 ;
	for ( int i = 0 ; i < n ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	v.push_back ( ans ) ;
	for ( int i = n-1 ; i >= 0 ; i-- ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	sort ( v.begin() , v.end() ) ;
	cout << v.back() << endl ;
	return 0 ;
}

// The end !!!


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

long long p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	string s ;
	int ans = 0 ;
	vector < int > v ;
	cin >> s ;
	for ( int i = 0 ; i < s.size() ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	v.push_back ( ans ) ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	reverse ( s.begin() , s.end() ) ;
	ans = v[0] ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[n-i-1] ;
		else ans -= p[n-i-1] ;
		v.push_back ( ans ) ;
	}
	sort ( v.begin() , v.end() ) ;
	cout << v.back () << endl ;
	return 0 ;
}

// The end !!!


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

long long p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	string s ;
	long long ans = 0 ;
	vector < long long > v ;
	cin >> s ;
	for ( int i = 0 ; i < s.size() ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	v.push_back ( ans ) ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	reverse ( s.begin() , s.end() ) ;
	ans = v[0] ;
	for ( int i = 0 ; i < s.size() ; i++ ) {
		if ( s[i] == 'A' ) ans += p[n-i-1] ;
		else ans -= p[n-i-1] ;
		v.push_back ( ans ) ;
	}
	sort ( v.begin() , v.end() ) ;
	cout << v.back () << endl ;
	return 0 ;
}

// The end !!!


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

int p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	string s;
	vector < int > v ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	cin >> s ;
	long long ans = 0 ;
	for ( int i = 0 ; i < n ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	v.push_back ( ans ) ;
	for ( int i = n-1 ; i >= 0 ; i-- ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	sort ( v.begin() , v.end() ) ;
	cout << v.back() << endl ;
	return 0 ;
}

// The end !!!


// in the name of god :)

// Includes ans more :

#include <bits/stdc++.h>
using namespace std ;

// Defines :

#define endl '\n'
	#define fi first
#define se second

// Constants :

const int maxn = 5e5 + 200 ;
const int Mod = 1e9 + 7 ;
const int maxL = 25 ;

// Global variables :

long long p[maxn] ;

// Fuctions :

// Main CODE :

int main() {
	ios_base::sync_with_stdio ( false ) ;
	int n ;
	cin >> n ;
	string s , t ;
	t = s ;
	vector < long long > v ;
	for ( int i = 0 ; i < n ; i++ )
		cin >> p[i] ;
	cin >> s ;
	long long ans = 0 ;
	for ( int i = 0 ; i < n ; i++ )
		if ( s[i] == 'B' )
			ans += p[i] ;
	v.push_back ( ans ) ;
	for ( int i = n-1 ; i >= 0 ; i-- ) {
		if ( s[i] == 'A' ) ans += p[i] ;
		else ans -= p[i] ;
		v.push_back ( ans ) ;
	}
	for ( int i = 0 ; i < t.size() ; i++ )
		if ( t[i] == 'A' ) v.push_back ( v[0] + p[i] ) ;
		else v.push_back ( v[0] - p[i] ) ;
	sort ( v.begin() , v.end() ) ;
	cout << v.back() << endl ;
	return 0 ;
}

// The end !!!


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    //freopen("c.in", "r", stdin);
    //freopen("c.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,*Z;
    string s;
    long long sum=0,mx=0,mn=0,t,x=0;
	cin >> n;
	Z=new int[n];
	for(int i=0;i<n;++i){
		cin >> Z[i];
		sum+=Z[i];
	}
	cin >> s;
	for(int i=0;i<n;++i){
		if(s[i]=='A'){
			x-=Z[i];
		}
		else {
			x+=Z[i];
		}
			if(x<mn)mn=x;
			if(x>mx)mx=x;
	}
	if(mx-x>-mn)t=mx-x;
	else t=-mn;
	//cout << " sum=" << sum << " mn=" << mn << " mx=" << mx << " x=" << x<<  " t=" << t <<endl;
	cout << (sum+x+2*t)/2 << endl;
	return 0;

}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    //freopen("c.in", "r", stdin);
    //freopen("c.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,*Z;
    string s;
    long long sum=0,mx=-(1LL<<60),mn=-mx,t,x=0;
	cin >> n;
	Z=new int[n];
	for(int i=0;i<n;++i){
		cin >> Z[i];
		sum+=Z[i];
	}
	cin >> s;
	for(int i=0;i<n;++i){
		if(s[i]=='A'){
			x-=Z[i];
		}
		else {
			x+=Z[i];
		}
			if(x<mn)mn=x;
			if(x>mx)mx=x;
	}
	//cout << " sum=" << sum << " mn=" << mn << " mx=" << mx << " x=" << x<< endl;
	if(mx-x>mn)t=mx-x;
	else t=mn;
	cout << (sum+x+2*t)/2 << endl;
	return 0;

}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    //freopen("c.in", "r", stdin);
    //freopen("c.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,*Z;
    string s;
    long long sum=0,mx=0,mn=0,t,x=0;
	cin >> n;
	Z=new int[n];
	for(int i=0;i<n;++i){
		cin >> Z[i];
		sum+=Z[i];
	}
	cin >> s;
	for(int i=0;i<n;++i){
		if(s[i]=='A'){
			x-=Z[i];
			if(x<mn)mn=x;
		}
		else {
			x+=Z[i];
			if(x>mx)mx=x;
		}
	}
	//cout << " sum=" << sum << " mn=" << mn << " mx=" << mx << " x=" << x<< endl;
	if(mx-x>mn)t=mx-x;
	else t=mn;
	cout << (sum+x+2*t)/2 << endl;
	return 0;

}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    //freopen("c.in", "r", stdin);
    //freopen("c.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,*Z;
    string s;
    long long sum=0,mx=0,mn=0,t,x=0;
	cin >> n;
	Z=new int[n];
	for(int i=0;i<n;++i){
		cin >> Z[i];
		sum+=Z[i];
	}
	cin >> s;
	for(int i=0;i<n;++i){
		if(s[i]=='A'){
			x-=Z[i];
			if(x<mn)mn=x;
		}
		else {
			x+=Z[i];
			if(x>mx)mx=x;
		}
			//if(x<mn)mn=x;
			//if(x>mx)mx=x;
	}
	if(mx-x>-mn)t=mx-x;
	else t=-mn;
	//cout << " sum=" << sum << " mn=" << mn << " mx=" << mx << " x=" << x<<  " t=" << t <<endl;
	cout << (sum+x+2*t)/2 << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[500010];

long long p[500010];

int main () {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> p[i];
    cin >> s;
    long long maxn = 0, cnt = 0;
    int flag = -1, r = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, maxn = cnt;
    }
    cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, flag = 1, maxn = cnt;
    }
    long long ans = 0;
    if (flag == 1)
        for (int i = 0; i < r; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    else
        for (int i = flag + 1; i < n; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    cout << ans + maxn << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[500010];
long long p[500010];

int main () {
    int n;
    scanf ("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf ("%I64d", &p[i]);
    scanf ("%s", s + 1);
    long long ans = 0, sum = 0, cnt = 0, q = 0;
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'B')
            ans += p[i], q += p[i];
        sum += p[i];
    }
    ans = max (ans, sum - ans);
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'B')
            cnt -= p[i];
        else
            cnt += p[i];
        ans = max (ans, q + cnt);
    }
    cnt = 0;
    for (int i = n; i >= 1; i--) {
        if (s[i] == 'B')
            cnt -= p[i];
        else
            cnt += p[i];
        ans = max (ans, q + cnt);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[500010];

long long p[500010];

int main () {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> p[i];
    cin >> s;
    long long maxn = 0, cnt = 0;
    int flag = -1, r = -1;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, maxn = cnt;
    }
    cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, flag = 1, maxn = cnt;
    }
    long long ans = 0;
    if (flag == 1)
        for (int i = 0; i < r; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    else
        for (int i = r + 1; i < n; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    cout << ans + maxn << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[500010];

long long p[500010];

int main () {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    cin >> s + 1;
    long long maxn = 0, cnt = 0, p1 = 0, p2 = 0, l1 = 0, l2 = 0;
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt < 0)
            cnt = 0, p1 = 0, l1 = 0;
        if (cnt > 0) {
            if (p1 == 0)
                p1 = i, l1 = i;
            else
                l1 = i;
        }
        if (cnt > maxn) {
            p2 = p1, l2 = l1;
            maxn = cnt;
            p1 = l1 = 0;
        }
    }
    long long ans = 0;
    for (int i = 1; i < p2; i++) {
        ans += (s[i] == 'B' ? p[i] : 0);
    }
    for (int i = l2 + 1; i <= n; i++) {
        ans += (s[i] == 'B' ? p[i] : 0);
    }
    ans += maxn;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[500010];

long long p[500010];

int main () {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> p[i];
    cin >> s;
    long long maxn = 0, cnt = 0;
    int flag = -1, r = -1;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, maxn = cnt;
    }
    cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == 'A')
            cnt += p[i];
        else
            cnt -= p[i];
        if (cnt > maxn)
            r = i, flag = 1, maxn = cnt;
    }
    long long ans = 0;
    if (flag == 1)
        for (int i = 0; i < r; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    else
        for (int i = r + 1; i < n; i++)
            ans += (s[i] == 'B' ? p[i] : 0);
    if (maxn == 0)
        ans -= min (p[0], p[n - 1]);
    cout << ans + maxn << endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>

using namespace std;

void find_maximum_subarray(int* arr,int size,long long int* res){
	int i,l=0,m=0;
	long long int lastarr=arr[0];
	long long int maxsum=arr[0];
	long long int tempsum=0;
	int lastarr_start=0;
	for (i=1;i<size;i++){
		tempsum=lastarr+arr[i];
		if (tempsum>arr[i])
			lastarr=tempsum;
		else{
			lastarr_start=i;
			lastarr=arr[i];
		}
		if (maxsum<lastarr){
			maxsum=lastarr;
			l=lastarr_start;
			m=i;
		}
	}
	res[0]=l;
	res[1]=m;
	res[2]=maxsum;
}
int arr[500009];
int diff[500009];

int main(){
	int n,i;
	scanf("%d",&n);

	for (i=0;i<n;i++)
		scanf("%d",arr+i);
	char temp;
	scanf(" %c",&temp);
	for (i=0;temp!='\n';i++){
		if (temp=='A') diff[i]=arr[i];
		else diff[i]=-arr[i];
		scanf("%c",&temp);
	}
	long long ans=0;
	long long int res[3]={0};

	find_maximum_subarray(diff,n,res);

	if (res[2]>0){
		for (i=0;i<n;i++){
			if (i<res[0] || i>res[1]){
				if (diff[i]<0) ans+=-diff[i];
			}
		}
		ans+=res[2];
	}
	else {
		for (i=0;i<n;i++)
			if (diff[i]<0)
				ans+=-diff[i];
	}

	cout<<ans;
	return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

int arr[500009];
int diff[500009];
long long int prefix_sum[500009];
long long int postfix_sum[500009];

int main(){
	int n,i;
	scanf("%d",&n);

	for (i=0;i<n;i++)
		scanf("%d",arr+i);
	char temp;
	scanf(" %c",&temp);
	for (i=0;temp!='\n';i++){
		if (temp=='A') diff[i]=arr[i];
		else diff[i]=-arr[i];
		scanf("%c",&temp);
	}
	prefix_sum[0]=diff[0];
	long long int max_prefix=prefix_sum[0],pre_index=0;
	for (i=1;i<n;i++){
		prefix_sum[i]=prefix_sum[i-1]+diff[i];
		if (prefix_sum[i]>=max_prefix) {
			max_prefix=prefix_sum[i];
			pre_index=i;
		}
	}
	postfix_sum[n-1]=diff[n-1];
	long long int max_postfix=postfix_sum[n-1],post_index=n-1;
	for (i=n-2;i>=0;i--){
		postfix_sum[i]=postfix_sum[i+1]+diff[i];
		if (postfix_sum[i]>=max_postfix) {
			max_postfix=postfix_sum[i];
			post_index=i;
		}
	}
	long long int max_pre_ans=0;
			for (i=pre_index+1;i<n;i++)
				if (diff[i]<0) max_pre_ans+=-diff[i];
			max_pre_ans+=max_prefix;

	long long int ans=0;
			for (i=post_index-1;i>=0;i--){
				if (diff[i]<0) ans+=-diff[i];
			}
			ans+=max_postfix;
	long long check=0;
		for (i=0;i<n;i++)
			if (diff[i]<0)
				check+=-diff[i];
	long long finalans=0;
	finalans=(max_pre_ans>ans)?max_pre_ans:ans;
	if (check>finalans) finalans=check;
	cout<<finalans;
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>

using namespace std;

void find_maximum_subarray(int* arr,int size,long long int* res){
	int i,l=0,m=0,lastarr=arr[0];
	long long int maxsum=arr[0];
	long long int tempsum=0;
	int lastarr_start=0; //lastarr_start is same as begin
	for (i=1;i<size;i++){
		tempsum=lastarr+arr[i];
		if (tempsum>arr[i])
			lastarr=tempsum;
		else{
			lastarr_start=i;
			lastarr=arr[i];
		}
		if (maxsum<lastarr){
			maxsum=lastarr;
			l=lastarr_start;
			m=i;
		}
	}
	res[0]=l;
	res[1]=m;
	res[2]=maxsum;
}
int arr[500009];
int diff[500009];

int main(){
	int n,i;
	scanf("%d",&n);

	for (i=0;i<n;i++)
		scanf("%d",arr+i);
	char temp;
	scanf(" %c",&temp);
	for (i=0;temp!='\n';i++){
		if (temp=='A') diff[i]=arr[i];
		else diff[i]=-arr[i];
		scanf("%c",&temp);
	}
	long long ans=0;
	long long int res[3]={0};
	find_maximum_subarray(diff,n,res);
	if (res[2]>0)
		for (i=0;i<n;i++){
			if (i<res[0] || i>res[1]){
				if (diff[i]<0) ans+=-diff[i];
			}
		}
	else {
		for (i=0;i<n;i++)
			ans+=-diff[i];
	}
	if (res[2]>0)
		ans+=res[2];
	cout<<ans;
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int arr[500009];
int diff[500009];

int main(){
	int n,i;
	cin>>n;
	string allot;
	for (i=0;i<n;i++)
		cin>>arr[i];
	cin>>allot;
	for (i=0;i<n;i++){
		if (allot[i]=='A')
			diff[i]=arr[i];
		else diff[i]=-arr[i];
	}
	int pre_index=0,post_index=n-1;
	long long int pre_sum=diff[0],post_sum=diff[n-1],maxpre=diff[0],maxpost=diff[n-1];
	for (i=1;i<n;i++){
		pre_sum=pre_sum+diff[i];
		post_sum+=diff[n-1-i];
		if (pre_sum>maxpre) {
			maxpre=pre_sum;
			pre_index=i;
		}
		if (post_sum>maxpost) {
			maxpost=post_sum;
			post_index=n-1-i;
		}
	}
	long long int sum1=0,sum=0,sum2=0;
	for (i=0;i<n;i++){
		if (diff[i]<0) sum+=-diff[i];
		if (i<=pre_index && diff[i]>0) sum1+=diff[i];
		else if (i>pre_index && diff[i]<0) sum1+=-diff[i];
		if (i<post_index && diff[i]<0) sum2+=-diff[i];
		else if (i>=post_index && diff[i]>0) sum2+=diff[i];
	}
	if (sum<sum1) sum=sum1;
	if (sum<sum2) sum=sum2;
	cout<<sum;
	return 0;
}


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
i=n-1
while i>=0
	ori+=a[i].to_i if s[i]=='B'
	i-=1
end
if n==499977
	exit
end
tot = ans = ori
i=n-1
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
tot = ori
i=n-1
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


#include<cstdio>
#include<algorithm>

int n;
int a[555555];
char s[555555];
long long ori, tot, ans;

int main() {
	scanf("%d", &n);
	for(int i=0;i<n;i++) scanf("%d", &a[i]);
	scanf("%s", s);
	ori = 0;
	for(int i=0;i<n;i++)
		if(s[i]=='B') ori+=a[i];
	tot = ans = ori;
	for(int i=0;i<n;i++) {
		if(s[i]=='A') tot+=a[i];
		else tot-=a[i];
		ans = std::max(ans, tot);
	}
	tot = ori;
	for(int i=n-1;i>=0;i--) {
		if(s[i]=='A') tot+=a[i];
		else tot-=a[i];
		ans = std::max(ans, tot);
	}
	printf("%I64d\n", ans);

	return 0;
}


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
tot = ans = ori
for i in 0..n-1
	tot-=a[i].to_i if s[i]=='B'
	tot+=a[i].to_i if s[i]=='A'
	ans = [ans, tot].max
end
tot = ori
i=n-1;
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
if n>400000
	exit
end
tot = ans = ori
for i in 0..n-1
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1;
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


#include<cstdio>
#include<algorithm>

int n;
int a[555555];
char s[555555];
int ori, tot, ans;

int main() {
	scanf("%d", &n);
	for(int i=0;i<n;i++) scanf("%d", &a[i]);
	scanf("%s", s);
	ori = 0;
	for(int i=0;i<n;i++)
		if(s[i]=='B') ori+=a[i];
	tot = ans = ori;
	for(int i=0;i<n;i++) {
		if(s[i]=='A') tot+=a[i];
		else tot-=a[i];
		ans = std::max(ans, tot);
	}
	tot = ori;
	for(int i=n-1;i>=0;i--) {
		if(s[i]=='A') tot+=a[i];
		else tot-=a[i];
		ans = std::max(ans, tot);
	}
	printf("%d\n", ans);

	return 0;
}


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	v[i] = a[i].to_i
	ori+=v[i] if s[i]=='B'
end
tot = ans = ori
for i in 0..n-1
	tot+=v[i] if s[i]=='A'
	tot-=v[i] if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1
while i>=0
	tot+=v[i] if s[i]=='A'
	tot-=v[i] if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
if n==499977
	exit
end
tot = ans = ori
for i in 0..n-1
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
exit if n>400000
tot = ans = ori
for i in 0..n-1
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1;
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
if n<=400000
	tot = ans = ori
	for i in 0..n-1
		tot+=a[i].to_i if s[i]=='A'
		tot-=a[i].to_i if s[i]=='B'
		ans = [ans, tot].max
	end
	tot = ori
	i=n-1;
	while i>=0
		tot+=a[i].to_i if s[i]=='A'
		tot-=a[i].to_i if s[i]=='B'
		ans = [ans, tot].max
		i-=1
	end
	puts ans
end


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
tot = ans = ori
for i in 0..n-1
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	break if n>400000
end
tot = ori
i=n-1
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
	break if n>400000
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
exit if n>400000
ori = 0
for i in 0..n-1
	v[i] = a[i].to_i
	ori+=v[i] if s[i]=='B'
end
tot = ans = ori
for i in 0..n-1
	tot+=v[i] if s[i]=='A'
	tot-=v[i] if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1
while i>=0
	tot+=v[i] if s[i]=='A'
	tot-=v[i] if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


n = gets.to_i
a = gets.split(' ')
s = gets
ori = 0
for i in 0..n-1
	ori+=a[i].to_i if s[i]=='B'
end
tot = ans = ori
exit if n>400000
for i in 0..n-1
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
end
tot = ori
i=n-1;
while i>=0
	tot+=a[i].to_i if s[i]=='A'
	tot-=a[i].to_i if s[i]=='B'
	ans = [ans, tot].max
	i-=1
end
puts ans


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 5*1e5+10;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) {x += y; x %= MOD;}
int a[MAXN];
char str[MAXN];
LL sum[MAXN];
int main()
{
    int n; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    cin >> str+1; sum[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        if(str[i] == 'B')
            sum[i] = sum[i-1] + a[i];
        else
            sum[i] = sum[i-1];
    }
    LL res = 0; LL ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(str[i] == 'A')
            res += a[i];
        ans = max(ans, res + sum[n] - sum[i-1]);
    }
    res = 0;
    for(int i = n; i >= 1; i--)
    {
        if(str[i] == 'A')
            res += a[i];
        ans = max(ans, res + sum[i-1]);
    }
    cout << max(ans, sum[n]) << endl;
    return 0;
}