/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        if(idx==0){cout<<a[0]<<nl;
        return;}
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<=idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ll mx=cnt[idx]-a[idx];
        ll g=a[idx];
        for(ll i=idx-1;i>0;i--)
        {
            if(s[i]=='1')
            {
                if((g+cnt[i-1])>mx)
                    mx=max(mx,g+cnt[i-1]);
                g+=a[i];
            }
        }
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        if(idx==0){cout<<a[0]<<nl;
        return;}
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<=idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ll mx=cnt[idx]-a[idx];
        ll g=a[idx];
        ll ans=0;
        F(i,idx+1){
            if(s[i]=='1')
                ans+=a[i];
        }
        mx=max(ans,mx);
        for(ll i=idx-1;i>0;i--)
        {
            if(s[i]=='1')
            {
                if((g+cnt[i-1])>mx)
                    mx=max(mx,g+cnt[i-1]);
                g+=a[i];
            }
        }
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        ll mx=INT_MIN;
        ll ans=0;
        for(ll i=0;i<idx;i++){
            ans+=a[i];
        }
        mx=max(ans,mx);
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<=idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ans=a[idx];
        for(ll i=idx-1;i>=0;i--)
        {
            if(s[i]=='1')ans+=a[i];
        }
        mx=max(ans,mx);
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        ll mx=INT_MIN;
        ll ans=0;
        for(ll i=0;i<idx;i++){
            ans+=a[i];
        }
        mx=max(ans,mx);
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ans=a[idx];
        for(ll i=idx-1;i>=0;i--)
        {
            if(s[i]=='0')
            {
                if(i>0 && cnt[i]>cnt[i-1])
                    {
                        ans+=a[i];
                        break;
                    }
            }
            else{
                ans+=a[i];
            }
        }
        mx=max(ans,mx);
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<=idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ll mx=cnt[idx]-a[idx];
        ll g=a[idx];
        for(ll i=idx-1;i>0;i--)
        {
            if(s[i]=='1')
            {
                if((g+cnt[i-1])>mx)
                    mx=max(mx,g+cnt[i-1]);
                g+=a[i];
            }
        }
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal !!!!!!!!!!!!!!!
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 1e7 + 2;
#define s second
#define f first
ll a[MAXN];
ll pre[MAXN];

int main() {
int n;
string s;
cin >> n;
for(int i = 0 ; i < n ; i++)
   cin >> a[i];

cin >> s;
pre[0] = 0;
for(int i = 0 ; i <= n ; i++)
  pre[i + 1] = pre[i - 1] + a[i];
ll ans = 0;
ll curr = 0;
for(int i = n - 1; i >= 0 ; i--)
{
    if(s[i] == '1')
        {
            ll tmp = curr + pre[i];
            ans = max(ans , tmp);
            curr=+a[i];

        }
}

cout << max(ans, curr);

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal !!!!!!!!!!!!!!!
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 1e7 + 2;
#define s second
#define f first
ll a[MAXN];
ll pre[MAXN];

int main() {
int n;
string s;
cin >> n;
for(int i = 0 ; i < n ; i++)
   cin >> a[n - i];

cin >> s;
reverse(s.begin(),s.end());
for(int i = 1 ; i <= n ; i++)
  pre[i] = pre[i - 1] + a[i];
ll ans = 0;
ll curr = 0;
for(int i = 1 ; i <= n ; i++)
{
    if(s[i-1] == '1')
        {
            ll tmp = curr + pre[n]  - pre[i];
            ans = max(ans , max(curr + a[i] , tmp));
            curr=+a[i];

        }
}

cout << ans ;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal !!!!!!!!!!!!!!!
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 1e7 + 2;
#define s second
#define f first
ll dp[MAXN][3];
ll a[MAXN];
ll n;
string s;
ll solve(int i,int exc)
{
   if(i == n)
       return 0;

   ll &ret = dp[i][exc];
   if(ret != -1)
       return ret;
    ret = 0;
   if(exc)
   {
       if(s[i] == '1')
          {
            ret = max(ret , solve(i + 1 , 1) + a[i]);
            ret = max(ret , solve(i + 1 , 0));
          }
      else
      {
          ret = max(ret , solve(i + 1 , 1));
      }
   }
   else
   {
       ret = max(ret , solve(i + 1 , 0) + a[i]);
       ret = max(ret , solve(i + 1,  0));
   }
   return ret;

}

int main() {

cin >> n;
for(int i = 0 ; i < n ; i++)
   cin >> a[i];

//reverse(a,a+n);
cin >> s;
reverse(s.begin(),s.end());
reverse(a,a+n);
memset(dp,-1,sizeof(dp));
cout << solve(0,1) ;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal !!!!!!!!!!!!!!!
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 1e7 + 2;
#define s second
#define f first
ll a[MAXN];
ll pre[MAXN];

int main() {
int n;
string s;
cin >> n;
for(int i = 0 ; i < n ; i++)
   cin >> a[n - i - 1];

cin >> s;
reverse(s.begin(),s.end());
pre[0] = a[0];
for(int i = 1 ; i <= n ; i++)
  pre[i] = pre[i - 1] + a[i];
ll ans = 0;
ll curr = 0;
for(int i = 0 ; i < n ; i++)
{
    if(s[i] == '1')
        {
            ll tmp = curr + pre[n] - pre[i];
            ans = max(ans , tmp);
            curr=+a[i];

        }
}

cout << max(ans, curr);

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal !!!!!!!!!!!!!!!
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 1e7 + 2;
#define s second
#define f first
ll a[MAXN];
ll pre[MAXN];

int main() {
int n;
string s;
cin >> n;
for(int i = 0 ; i < n ; i++)
   cin >> a[n - i - 1];

cin >> s;
reverse(s.begin(),s.end());
pre[0] = a[0];
for(int i = 1 ; i <= n ; i++)
  pre[i] = pre[i - 1] + a[i];
ll ans = 0;
ll curr = 0;
for(int i = 0 ; i < n - 1; i++)
{
    if(s[i] == '1')
        {
            ll tmp = curr + pre[n] - pre[i];
            ans = max(ans , tmp);
            curr=+a[i];

        }
}

cout << max(ans, curr);

}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
vvi adj(maxn);
ll n,m;
ll ans=0;
vi a(maxn);
ll cnt=0;
void dfs(ll v,ll pr,ll k){
    if(k>m)return;
    int cnt=1;
    for(int i=0;i<sz(adj[v]);i++){
        if(adj[v][i]!=pr){
            cnt=0;
            dfs(adj[v][i],v,k*a[adj[v][i]]+a[adj[v][i]]);
        }
    }
    ans+=cnt;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        cin>>s;
        ll ans=0;
        ll tot=0;
        for(int i=0;i<n;i++){
            tot+=a[i];
        }
        for(int i=n-1;i>=0;i--){
            if(s[i]=='1')ans=max(ans,tot-a[i]);
            else tot-=a[i];
        }
        cout<<ans;
    }
   return 0;
}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
vvi adj(maxn);
ll n,m;
ll ans=0;
vi a(maxn);
ll cnt=0;
void dfs(ll v,ll pr,ll k){
    if(k>m)return;
    int cnt=1;
    for(int i=0;i<sz(adj[v]);i++){
        if(adj[v][i]!=pr){
            cnt=0;
            dfs(adj[v][i],v,k*a[adj[v][i]]+a[adj[v][i]]);
        }
    }
    ans+=cnt;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        cin>>s;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(s[i]=='1')ans+=a[i];
        }
       // cout<<ans<<"\n";
        int ind=-1;
        for(int i=n-1;i>=0;i--){
            if(s[i]=='1'){
                ind=i;
                break;
            }
        }
        for(int i=1;i<n;i++)a[i]+=a[i-1];
        //for(int i=0;i<n;i++)cout<<a[i]<<"\n";
        for(int i=n-1;i>0;i--){
            if(i==n-1)continue;
            if(s[i]=='1'){
                ll x=a[i+1]-a[i]+a[i-1];
                ans=max(ans,x);
            }
        }
        cout<<ans;
    }
   return 0;
}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
vvi adj(maxn);
ll n,m;
ll ans=0;
vi a(maxn);
ll cnt=0;
void dfs(ll v,ll pr,ll k){
    if(k>m)return;
    int cnt=1;
    for(int i=0;i<sz(adj[v]);i++){
        if(adj[v][i]!=pr){
            cnt=0;
            dfs(adj[v][i],v,k*a[adj[v][i]]+a[adj[v][i]]);
        }
    }
    ans+=cnt;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        cin>>s;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(s[i]=='1')ans+=a[i];
        }
       // cout<<ans<<"\n";
        int ind=-1;
        for(int i=n-1;i>=0;i--){
            if(s[i]=='1'){
                ind=i;
                break;
            }
        }
        for(int i=1;i<n;i++)a[i]+=a[i-1];
        //for(int i=0;i<n;i++)cout<<a[i]<<"\n";
        for(int i=n-1;i>0;i--){
            if(i==n-1)continue;
            if(s[i]=='1' and s[i+1]=='1'){
                ll x=a[i+1]-a[i]+a[i-1];
                ans=max(ans,x);
            }
            else ans=max(ans,a[i-1]);
        }
        cout<<ans;
    }
   return 0;
}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
vvi adj(maxn);
ll n,m;
ll ans=0;
vi a(maxn);
ll cnt=0;
void dfs(ll v,ll pr,ll k){
    if(k>m)return;
    int cnt=1;
    for(int i=0;i<sz(adj[v]);i++){
        if(adj[v][i]!=pr){
            cnt=0;
            dfs(adj[v][i],v,k*a[adj[v][i]]+a[adj[v][i]]);
        }
    }
    ans+=cnt;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        cin>>s;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(s[i]=='1')ans+=a[i];
        }
        int ind=-1;
        for(int i=n-1;i>=0;i--){
            if(s[i]=='1'){
                ind=i;
                break;
            }
        }
        for(int i=1;i<n;i++)a[i]+=a[i-1];
        if(ind==-1){
            cout<<ans;
            return 0;
        }
        for(int i=ind-1;i>=0;i--){
            ans=max(ans,a[i]);
        }
        cout<<ans;
    }
   return 0;
}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        vi a(n);
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        ll ans=0;
        ll tot=0;
        for(int i=0;i<n;i++){
            tot+=a[i];
        }
        //cout<<tot<<"\n";
        for(int i=n-1;i>=0;i--){
            //cout<<a[i]<<"\n";
            if(s[i]=='1'){
                ll x=tot-a[i];
                //cout<<x<<"\n";
                ans=max(ans,x);
            }
            else tot-=a[i];
        }
        cout<<max(ans,tot);
    }
   return 0;
}


// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
//If you are not doing well now just stick to it and learn new things one day you will be...
//Compete with yourself
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define trav(a,x) for(auto &a:x)
#define fr(i,a,b)  for(ll i=a;i<=b;i++)
#define fr1(i,a)   for(ll i=0;i<a;i++)
#define frr(i,a,b) for(ll i=b;i>=a;i--)
#define frr1(i,a)   for(ll i=a-1;i>=0;i--)
#define sorta(a,n) sort(a,a+n)
#define sortd(a,n) sort(a,a+n,greater<ll>())
#define sorva(a) sort(a.begin(),a.end())
#define sortvd(a) sort(a.begin(),a.end(),greater<ll>())
#define tc(t) while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
using vi= vector<ll>;
using vvi= vector<vi>;
using vb =vector<bool>;
using vc =vector<char>;
using vs =vector<string>;
using vld =vector<ld>;
using pii =pair<ll,ll>;
using psi =pair<string,ll>;
using pci =pair<char,ll>;
using vpii =vector<pii>;
//}}}template*/
ll mod=1e9+7;
ll const maxn=1e5+5;
ll const inf=1e18;
ll add(ll a,ll b){
    return ((a%mod)+(b%mod))%mod;
}
ll mul(ll a,ll b){
    return ((a%mod)*(b%mod))%mod;
}
ll powm(ll x,ll n,ll M)
{
    ll result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%M;
        if(n<2)break;
        x=(x*x)%M;
        n=n/2;
    }
    return result;
}
ll modinverse(ll a, ll m){
    return powm(a,m-2,m);
}
bool prime(ll x){
    if(x<=1)return false;
    for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;
    return true;
}
vi divisor(ll x){
    ll cnt=0;
    vi v;
    for(ll i=1;i<=sqrt(x);i++){
        if(x%i==0){
            if(i!=x/i){v.pb(i);v.pb(x/i);}
            else v.pb(max(i,x/i));
        }
    }
    return v;
}
vector<ll> sieve(ll n) {
    bool prim[n+1];
    memset(prim, true, sizeof(prim));

    for (ll p=2; p*p<=n; p++) {
        if (prim[p] == true) {
            for (int i=p*p; i<=n; i += p)
                prim[i] = false;
        }
    }
    vector<ll>v;
    for(int i=2;i<=n;i++)if(prim[i])v.push_back(i);
    return v;
}
vvi adj(maxn);
ll n,m;
ll ans=0;
vi a(maxn);
ll cnt=0;
void dfs(ll v,ll pr,ll k){
    if(k>m)return;
    int cnt=1;
    for(int i=0;i<sz(adj[v]);i++){
        if(adj[v][i]!=pr){
            cnt=0;
            dfs(adj[v][i],v,k*a[adj[v][i]]+a[adj[v][i]]);
        }
    }
    ans+=cnt;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fio;
    ll t;
    t=1;
  //  cin>>t;
    tc(t){
        ll n;
        cin>>n;
        fr1(i,n)cin>>a[i];
        string s;
        cin>>s;
        cin>>s;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(s[i]=='1')ans+=a[i];
        }
       // cout<<ans<<"\n";
        int ind=-1;
        for(int i=n-1;i>=0;i--){
            if(s[i]=='1'){
                ind=i;
                break;
            }
        }
        vi b(n);
        fr1(i,n){
            if(s[i]=='1')b[i]=a[i];
            else b[i]=0;
        }
        for(int i=1;i<n;i++)a[i]+=a[i-1];
        //for(int i=1;i<n;i++)b[i]+=b[i-1];
        //for(int i=0;i<n;i++)cout<<a[i]<<"\n";
        for(int i=ind-1;i>0;i--){
            ll x=0;
            if(s[i]=='1'){
                x=b[ind]-b[i]+a[i];
                ans=max(ans,x);
            }
            else ans=max(ans,a[i]);
        }
        cout<<ans;
    }
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, A[MAXN];
string s;

int main() {
	cin>>n;
	for (int i = 0; i < n; i++) {
		cin>>A[i];
	}
	cin>>s;
	long long int ans=0, b=0;
	for (int i = 0; i < n; i++) {
		if (s[i] == '1')
		ans = max(ans + A[i], b);
		b+= A[i];
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e8;
int m=0,n;
string s;
long long int A[maxn];

void make_num()
{
	int tmp;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='1')
		{
			tmp=pow(2,i);
			m+=tmp;
		}
	}
}

int bit(int x,int i)
{
	int bit=x&(1<<i);
	return min(bit,1);
}

int function(int x)
{
	int f=0;
	for(int i=0;i<n;i++)
	{
		f+=A[i]*bit(x,i);
	}
	return f;
}
int main()
{

	int i,ans=0,tmp;
	cin>>n;

	for(i=0;i<n;i++)
	cin>>A[i];
	cin>>s;
	make_num();
	for(i=0;i<=m;i++)
	{
		tmp=function(i);
		ans=max(ans,tmp);
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e8;
int m=0,n;
string s;
long long int A[maxn];

void make_num()
{
	int tmp;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='1')
		{
			tmp=pow(2,i);
			m+=tmp;
		}
	}
}

int bit(int x,int i)
{
	int bit=x&(1<<i);
	return min(bit,1);
}

int funny(int x)
{
	int f=0;
	for(int i=0;i<n;i++)
	{
		f+=A[i]*bit(x,i);
	}
	return f;
}
int main()
{

	int i,ans=0,tmp;
	cin>>n;

	for(i=0;i<n;i++)
	cin>>A[i];
	cin>>s;
	make_num();
	for(i=0;i<=m;i++)
	{
		tmp=funny(i);
		ans=max(ans,tmp);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int
main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	vector<int> vec(n);
	for(int i = 0; i < n; i++) cin >> vec[i];
	string str; cin >> str;
	int ans = 0;
	map<int, int> mp;
	for(int i = vec.size() - 1; i >= 0; i--){
		if(str[i] == '1'){
			ans += vec[i];
			mp[vec[i]]++;
		}
		else if(str[i] == '0'){
			if(mp.size() == 0) continue;
			int key = mp.begin()->first;
			if(key < vec[i]){
				mp[key]--;
				if(mp[key] == 0) mp.erase(mp.begin());
				ans -= key;
				ans += vec[i];
			}
		}
	}
	cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int
main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	vector<int> vec(n);
	for(int i = 0; i < n; i++) cin >> vec[i];
	string str; cin >> str;
	vector<int> sum(n);
	for(int i = 0; i < n; i++){
		if(i - 1 >= 0) sum[i] = sum[i - 1] + vec[i];
		else sum[i] = vec[i];
	}
	int add = 0;
	int ans = 0;
	for(int i = n - 1; i >= 0; i--){
		if(str[i] == '1'){
			if(i - 1 >= 0)
				ans = max(ans, add + sum[i - 1]);
			else ans = max(ans, add + vec[i]);
			add += vec[i];
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

int
main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	vector<int> vec(n);
	for(int i = 0; i < n; i++) cin >> vec[i];
	string str; cin >> str;
	vector<int> sum(n);
	for(int i = 0; i < n; i++){
		if(i - 1 >= 0) sum[i] = sum[i - 1] + vec[i];
		else sum[i] = vec[i];
	}
	int add = 0;
	int ans = INT_MIN;
	for(int i = n - 1; i >= 0; i--){
		if(str[i] == '1'){
			if(i - 1 >= 0)
				ans = max(ans, add + sum[i - 1]);
			else ans = max(ans, add + vec[i]);
			add += vec[i];
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

int
main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	vector<int> vec(n);
	for(int i = 0; i < n; i++) cin >> vec[i];
	string str; cin >> str;
	int ans = 0;
	map<int, int> mp;
	for(int i = vec.size() - 1; i >= 0; i--){
		if(str[i] == '1'){
			ans += vec[i];
			mp[vec[i]]++;
		}
		else if(str[i] == '0'){
			if(mp.size() == 0) continue;
			int key = mp.begin()->first;
			if(key < vec[i]){
				mp[key]--;
				if(mp[key] == 0) mp.erase(mp.begin());
				mp[vec[i]]++;
				ans -= key;
				ans += vec[i];
			}
		}
	}
	cout << ans;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i<=b; ++i)
#define repp(i,b,a) for(int i=b; i>=a; --i)
#define mp make_pair
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
#define de(a) cout <<#a <<" => "<<a<<endl
#define dep(a, b) cout <<"("<<a<<","<<b<<")"<<endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int>vi;

const int maxn = 1e5+7;
int a[maxn];
char s[maxn];
int main(){
	//freopen("in.txt", "r", stdin);
	int n, x;
	scanf("%d", &n);
	rep(i, 1, n ) {
		scanf("%d", &x);
		a[i] = a[i-1] + x;
	}
	scanf("%s",s);
	int len = strlen(s) - 1;
	while(s[len] !='1' && len>=0) len --;
	if(len < 0) puts("0");
	else if(len == 0) printf("%d\n", a[1]);
	else {
		int MAX = a[len-1];
		int sum = a[len] - a[len-1];;
		repp(i, len-1, 0) {
			if(s[i] == '1') {
				int tmp = sum + a[i-1];
				MAX = max(tmp, MAX);
				sum += a[i]-a[i-1];
			}
		}
		printf("%d\n", MAX);
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i<=b; ++i)
#define repp(i,b,a) for(int i=b; i>=a; --i)
#define mp make_pair
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
#define de(a) cout <<#a <<" => "<<a<<endl
#define dep(a, b) cout <<"("<<a<<","<<b<<")"<<endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int>vi;

const int maxn = 2e2+7;
struct node{
	int v, id;
}p[maxn];
bool mark[maxn];
bool vis[maxn];
int ans[maxn];
bool cmp(node a, node b) {
	return a.v < b.v;
}
int main(){
	//freopen("in.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	n<<=1;
	int cnt = 0;
	rep(i, 1, n) {
		scanf("%d", &p[i].v);
		p[i].id = i;
		if(!mark[p[i].v] ) cnt++;
		mark[p[i].v] = 1;
	}
	sort(p+1, p+1+n, cmp);
	rep(i, 1, n) {
		if(i%2 == 0) ans[p[i].id] =1;
		else ans[p[i].id] =2;
	}
	ms(mark, 0);
	ms(vis, 0);
	int a=0, b=0;
	rep(i, 1, n) {
		if(ans[p[i].id] == 1) {
			if(!mark[p[i].v]) a++;
			mark[p[i].v] = 1;
		} else {
			if(!vis[p[i].v]) b++;
			vis[p[i].v] = 1;
		}
	}
//	de(a), de(b);
	printf("%d\n", a*b);
	rep(i, 1, n-1){
		printf("%d ", ans[i]);
	}
	printf("%d\n", ans[n]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i<=b; ++i)
#define per(i,b,a) for(int i=b; i>=a; --i)
#define mp make_pair
#define pb push_back
#define ms(a, b) memset(a, b, sizeof(a))
#define de(a) cout <<#a <<" => "<<a<<endl
#define dep(a, b) cout <<"("<<a<<","<<b<<")"<<endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int>vi;
const int maxn = 1e5+7;
const int inf =1e9;
// ----------
int n;
int a[maxn];
char s[maxn];
int main(){
	scanf("%d", &n);
	ll sum = 0;
	rep(i, 1, n) {
		scanf("%d", &a[i]);
		a[i]+=a[i-1];
	}
	scanf("%s", s+1);
	int ans = -1;
	int tmp =0;
	per(i, n, 1) {
		if(s[i] == '1') {
			ans = max(ans, tmp+a[i-1]);
			tmp+=a[i]-a[i-1];
		}
	}
	printf("%d\n", max(ans, tmp));
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
set<int> s;vector<int> v;
bool prime[mx];
int a [100050];
int sum [100050];
char b [100050];
void seive(int n)
{ memset(prime, true, sizeof(prime));
  for (int p=2; p*p<=n; p++){
        if (prime[p] == true) {
           for (int i=p*p; i<=n; i += p)
                prime[i] = false; }}}
void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}

signed main()
{  fun();

int n;
cin>>n;
ainp(a,n);
int maxpos = -1;
cin>>b;
sum [0] = a [0];
for (int i = 1; i <n; i ++)
{
	sum [i] = sum [i-1] + a [i];
}
for (int i = 0; i <n; i ++)
{
	if (b [i] == '1') maxpos = i;
}
int summ = 0, ans = 0;
for (int i = maxpos; i>=0; i--)
{
	if (b [i] == '1')
	{
		ans = max (ans, summ + sum [i-1]);
		summ+=a [i];
	}
}
cout<<max (ans, summ);
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
set<int> s;vector<int> v;
int ar[100000];
string s;
void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
int solve(int n,int f[])
{
	if(n==0&&s[n]=='0')
	{
		return 0;
	}
	if(n==0&&s[n]=='1')
	{
		return f[0];
	}
	if(s[n]=='0')
	{
		//cout<<f[n]<<endl;
		return solve(n-1,f);
	}
	//cout<<f[n]<<endl;
	return max(ar[n]+solve(n-1,f),f[n-1]);
}

signed main()
{  fun();
	int n;
	cin>>n;

	int sum[n];
	memset(sum,0,n);
	ainp(ar,n);
	sum[0]=ar[0];
	for(int i=1;i<n;i++)
	{
		sum[i]=ar[i]+sum[i-1];
		//cout<<sum[i]<<endl;
		//cout<<ar[i]<<endl;
	}

	cin>>s;
	int res=solve(n-1,sum);
	cout<<res;

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
set<int> s;vector<int> v;
bool prime[mx];
int ar[1000];
void seive(int n)
{ memset(prime, true, sizeof(prime));
  for (int p=2; p*p<=n; p++){
        if (prime[p] == true) {
           for (int i=p*p; i<=n; i += p)
                prime[i] = false; }}}
void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
int solve(string s,int n,int f[])
{
	if(n==0&&s[n]=='0')
	{
		return 0;
	}
	if(n==0&&s[n]=='1')
	{
		return f[0];
	}
	if(s[n]=='0')
	{
		//cout<<f[n]<<endl;
		return solve(s,n-1,f);
	}
	//cout<<f[n]<<endl;
	return max(ar[n]+solve(s,n-1,f),f[n-1]);
}

signed main()
{  fun();
	int n;
	cin>>n;

	int sum[n];
	memset(sum,0,n);
	ainp(ar,n);
	sum[0]=ar[0];
	for(int i=1;i<n;i++)
	{
		sum[i]=ar[i]+sum[i-1];
		//cout<<sum[i]<<endl;
		//cout<<ar[i]<<endl;
	}
	string s;
	cin>>s;
	int res=solve(s,n-1,sum);
	cout<<res;

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
set<int> s;vector<int> v;
int ar[100000];

void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
int solve(string s,int n,int f[])
{
	if(n==0&&s[n]=='0')
	{
		return 0;
	}
	if(n==0&&s[n]=='1')
	{
		return f[0];
	}
	if(s[n]=='0')
	{
		//cout<<f[n]<<endl;
		return solve(s,n-1,f);
	}
	//cout<<f[n]<<endl;
	return max(ar[n]+solve(s,n-1,f),f[n-1]);
}

signed main()
{  fun();
	int n;
	cin>>n;

	int sum[n];
	memset(sum,0,n);
	ainp(ar,n);
	sum[0]=ar[0];
	for(int i=1;i<n;i++)
	{
		sum[i]=ar[i]+sum[i-1];
		//cout<<sum[i]<<endl;
		//cout<<ar[i]<<endl;
	}
	string s;
	cin>>s;
	int res=solve(s,n-1,sum);
	cout<<res;

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
//set<int> s;vector<int> v;
int ar[100000];
string s;
void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
int solve(int n,int f[])
{
	if(n==0&&s[n]=='0')
	{
		return 0;
	}
	if(n==0&&s[n]=='1')
	{
		return f[0];
	}
	if(s[n]=='0')
	{
		//cout<<f[n]<<endl;
		return solve(n-1,f);
	}
	//cout<<f[n]<<endl;
	return max(ar[n]+solve(n-1,f),f[n-1]);
}

signed main()
{  fun();
	int n;
	cin>>n;

	int sum[n];
	memset(sum,0,n);
	ainp(ar,n);
	sum[0]=ar[0];
	for(int i=1;i<n;i++)
	{
		sum[i]=ar[i]+sum[i-1];
		//cout<<sum[i]<<endl;
		//cout<<ar[i]<<endl;
	}

	cin>>s;
	int res=solve(n-1,sum);
	cout<<res;

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mx 100000000
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=0;i<n;i++) cin>>arr[i];}
void vinp(vector<int> v,int n){for(int i=0;i<n;i++){int a;cin>>a;v.push_back(a);}}
map<int, int> mapp;
set<int> s;vector<int> v;
bool prime[mx];
int ar[100000];
void seive(int n)
{ memset(prime, true, sizeof(prime));
  for (int p=2; p*p<=n; p++){
        if (prime[p] == true) {
           for (int i=p*p; i<=n; i += p)
                prime[i] = false; }}}
void fun()
{ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
int solve(string s,int n,int f[])
{
	if(n==0&&s[n]=='0')
	{
		return 0;
	}
	if(n==0&&s[n]=='1')
	{
		return f[0];
	}
	if(s[n]=='0')
	{
		//cout<<f[n]<<endl;
		return solve(s,n-1,f);
	}
	//cout<<f[n]<<endl;
	return max(ar[n]+solve(s,n-1,f),f[n-1]);
}

signed main()
{  fun();
	int n;
	cin>>n;

	int sum[n];
	memset(sum,0,n);
	ainp(ar,n);
	sum[0]=ar[0];
	for(int i=1;i<n;i++)
	{
		sum[i]=ar[i]+sum[i-1];
		//cout<<sum[i]<<endl;
		//cout<<ar[i]<<endl;
	}
	string s;
	cin>>s;
	int res=solve(s,n-1,sum);
	cout<<res;

}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                1000000000000000001
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<ll,pp(ll) > p1, const pair<ll,pp(ll) > p2){
        return (p1.first ==  p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
    }
};

int main(){
    FIO;
    ll n; cin >> n;
    ll arr[n];
    forn(i,0,n) cin >> arr[i];
    string s; cin >> s;
    ll dp1[n] = {0}, dp2[n] = {0};
    if(s[0] == '1'){
        dp1[0] = arr[0];
    }
    dp2[0] = arr[0];
    forn(i,1,n){
        ll temp = 0;
        if(s[i] == '1'){
            if(s[i-1] == '0'){
                temp = max(temp, dp1[i-1]+arr[i]);
                temp = max(temp, dp2[i-1]);
            }else{
                temp = max(temp,dp1[i-1]+arr[i]);
            }
            dp1[i] = temp;
        }else{
            dp1[i] = dp1[i-1];
        }
        dp2[i] = dp2[i-1]+arr[i];
    }
    //dbgArr(dp1,n);
    //dbgArr(dp2,n);
    cout << dp1[n-1] << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                1000000000000000001
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<ll,pp(ll) > p1, const pair<ll,pp(ll) > p2){
        return (p1.first ==  p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
    }
};
ll dp1[100001] = {0};
ll rec(ll arr[], ll n ,string &s, ll carry, ll i){
    if(i<0 ) return 0;
    ll ans = 0;
    if(s[i] == '1'){

        ans = arr[i]+rec(arr, n ,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,2,i-1));
    }else if(carry > 0){
        ans = arr[i]+rec(arr,n,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,carry,i-1));
    }else{
        ans =  rec(arr,n,s,carry,i-1);
    }
    return dp1[i] = ans;
}
int main(){
    FIO;
    ll n; cin >> n;
    ll arr[n];
    forn(i,0,n) cin >> arr[i];
    string s; cin >> s;

    ll ans = rec(arr, n , s, 0,n-1);

    //dbgArr(dp1,n);
    //dbgArr(dp2,n);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                1000000000000000001
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<ll,pp(ll) > p1, const pair<ll,pp(ll) > p2){
        return (p1.first ==  p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
    }
};
ll dp1[100001][2] = {0};
ll rec(ll arr[], ll n ,string &s, ll carry, ll i){
    if(i<0 ) return 0;
    if(dp1[i][carry] != -1) return dp1[i][carry];
    ll ans = 0;
    if(s[i] == '1'){
        ans = arr[i]+rec(arr, n ,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,1,i-1));
    }else if(carry > 0){
        ans = arr[i]+rec(arr,n,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,carry,i-1));
    }else{
        ans =  rec(arr,n,s,carry,i-1);
    }
    return dp1[i][carry] = ans;
}
int main(){
    FIO;
    ll n; cin >> n;
    ll arr[n];
    forn(i,0,n) cin >> arr[i];
    string s; cin >> s;

    ll ans = rec(arr, n , s, 0,n-1);

    //dbgArr(dp1,n);
    //dbgArr(dp2,n);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                1000000000000000001
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<ll,pp(ll) > p1, const pair<ll,pp(ll) > p2){
        return (p1.first ==  p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
    }
};
ll dp1[100001][2] = {0};
ll rec(ll arr[], ll n ,string &s, ll carry, ll i){
    if(i<0 ) return 0;
    if(dp1[i][carry] != -1) return dp1[i][carry];
    ll ans = 0;
    if(s[i] == '1'){
        ans = arr[i]+rec(arr, n ,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,1,i-1));
    }else if(carry > 0){
        ans = arr[i]+rec(arr,n,s,carry,i-1);
        ans = max(ans,rec(arr,n,s,carry,i-1));
    }else{
        ans =  rec(arr,n,s,carry,i-1);
    }
    return dp1[i][carry] = ans;
}
int main(){
    FIO;
    ll n; cin >> n;
    ll arr[n];
    memset(dp1,-1,sizeof(dp1));
    forn(i,0,n) cin >> arr[i];
    string s; cin >> s;

    ll ans = rec(arr, n , s, 0,n-1);

    //dbgArr(dp1,n);
    //dbgArr(dp2,n);
    cout << ans << endl;
    return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Abukhadijah
// ~/Remember,remember the 6th of March
#include <functional>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<complex>
#include <numeric>
#include <string>
#include<iostream>
#include <fstream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include<cmath>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
const double PI = acos(-1.0);
const double  EPS = 1e-11;
typedef long long ll;
typedef unsigned long long ull;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdio);
void fast()
{
	std::ios_base::sync_with_stdio(0);
}
int n;
vector<int>arr;
string s;
vector<vector<int>>dp;
int calc(int i, int state)
{
	if (i == n)
		return 0;
	int &ret = dp[i][state];
	if (ret != -1)
	{
		return ret;
	}
	if (state == 1)
		return ret = calc(i + 1, 1) + arr[i];
	if (s[i] == '0')
		return ret = calc(i + 1, 0);
	int x = calc(i + 1, 0) + arr[i];
	int y = calc(i + 1, 1);
	return ret = max(x, y);
}
int main()
{
	scanf("%d", &n);
	arr.resize(n);
	dp.resize(n);
	for (int i = 0; i < n; i++)
		dp[i].resize(2, -1);
	for (int &i : arr)scanf("%d", &i);
	cin >> s;
	reverse(arr.begin(), arr.end());
	reverse(s.begin(), s.end());
	printf("%d\n", calc(0, 0));
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Abukhadijah
// ~/Remember,remember the 6th of March
#include <functional>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<complex>
#include <numeric>
#include <string>
#include<iostream>
#include <fstream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include<cmath>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
const double PI = acos(-1.0);
const double  EPS = 1e-11;
typedef long long ll;
typedef unsigned long long ull;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdio);
void fast()
{
	std::ios_base::sync_with_stdio(0);
}
int main()
{
	int n;
	cin >> n;
	vector<int>arr(n);
	for (int &i : arr)scanf("%d", &i);
	string s;
	cin >> s;
	vector<int>cs(n + 1);
	for (int i = 0; i < n; i++)
		cs[i + 1] = cs[i] + arr[i];
	int mx=0, sum=0;
	for (int i = s.length() - 1; i >= 0; i--)
	{
		if (s[i] == '0')continue;
		mx = max(cs[i] + sum, mx);
		sum += arr[i];
	}
	printf("%d\n", max(mx, sum));
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Abukhadijah
// ~/Remember,remember the 6th of March
#include <functional>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<complex>
#include <numeric>
#include <string>
#include<iostream>
#include <fstream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include<cmath>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
const double PI = acos(-1.0);
const double  EPS = 1e-11;
typedef long long ll;
typedef unsigned long long ull;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdio);
void fast()
{
	std::ios_base::sync_with_stdio(0);
}
int n;
vector<int>arr;
string s;
int calc(int i, int state)
{
	if (i == n)
		return 0;
	if (state == 1)
		return calc(i + 1, 1) + arr[i];
	if (s[i] == '0')
		return calc(i + 1, 0);
	return max(calc(i + 1, 0) + arr[i], calc(i + 1, 1));
}
int main()
{
	scanf("%d",&n);
	arr.resize(n);
	for (int &i : arr)scanf("%d", &i);
	cin >> s;
	reverse(arr.begin(), arr.end());
	reverse(s.begin(), s.end());
	printf("%d\n",calc(0, 0));
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Abukhadijah
// ~/Remember,remember the 6th of March
#include <functional>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<complex>
#include <numeric>
#include <string>
#include<iostream>
#include <fstream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include<cmath>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
const double PI = acos(-1.0);
const double  EPS = 1e-11;
typedef long long ll;
typedef unsigned long long ull;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdio);
void fast()
{
	std::ios_base::sync_with_stdio(0);
}
int main()
{
	int n;
	scanf("%d", &n);
	vector<int>arr(n);
	for (int &i : arr)scanf("%d", &i);
	multiset<int>st;
	string s;
	cin >> s;
	ll sum = 0;
	multiset<int>::iterator it = st.end();
	for (int i=0; i <s.length(); i++)
	{
		st.insert(arr[i]);
		if (s[i] == '1')
		{
			it = st.end();
			it--;
			sum += *it;
			st.erase(it);
		}
	}
	printf("%d\n", sum);
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Abukhadijah
// ~/Remember,remember the 6th of March
#include <functional>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<complex>
#include <numeric>
#include <string>
#include<iostream>
#include <fstream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include<cmath>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
const double PI = acos(-1.0);
const double  EPS = 1e-11;
typedef long long ll;
typedef unsigned long long ull;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdio);
void fast()
{
	std::ios_base::sync_with_stdio(0);
}
int n;
vector<int>arr;
string s;
vector<vector<int>>dp;
int calc(int i, int state)
{
	if (i == n)
		return 0;
	int &ret = dp[i][state];
	if (ret != -1)
		return ret;
	if (state == 1)
		return ret = calc(i + 1, 1) + arr[i];
	if (s[i] == '0')
		return ret= calc(i + 1, 0);
	return ret= max(calc(i + 1, 0) + arr[i], calc(i + 1, 1));
}
int main()
{
	scanf("%d",&n);
	arr.resize(n);
	dp.resize(n);
	for (int i = 0; i < n; i++)
		dp[i].resize(2, -1);
	for (int &i : arr)scanf("%d", &i);
	cin >> s;
	reverse(arr.begin(), arr.end());
	reverse(s.begin(), s.end());
	printf("%d\n",calc(0, 0));
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
ll arr[100005];
ll pre[100005];
ll cnt ;
int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	int k = st.size() - 1;
	while(st[k] == '0') k--;
	ll ans = pre[k];
	ll ans2 = arr[k + 1];
	for(int i = 0 ; i < k ; i++){
		cnt += (st[i] == '0');
	}
	ll tot = k + 1 - cnt;
	k--;
	while(st[k] == '0') k--;
	for(int i = 0 ; i <= k ; i++){
		v.pb(arr[i + 1]);
	}
	sort(all(v));
	reverse(all(v));
	for(auto x : v){
		if(tot == 0) break;
		ans2 += x;
		tot --;
	}
	cout << max(ans , ans2) << endl;
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
ll arr[100005];
ll pre[100005] , suf[100005];
ll cnt , ans ;
int main()
{
	int n;
	cin >> n;
	for(int i = 0 ; i < n; i++) cin >> arr[i];
	pre[0] = arr[0];
	for(int i = 1; i < n; i++){
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	suf[n] = 0;
	for(int i = n - 1; i >= 0 ; i--){
		if(st[i] == '1') {
			suf[i] = suf[i + 1] + arr[i];
		}
		else suf[i] = suf[i + 1];
	}
	ll cum = 0;
	for(int i = 0; i < n ; i ++){
		if(st[i] == '1'){
			cum += arr[i];
		}
		if(st[i] == '0') ans = max(ans , cum + suf[i + 1]);
		else {
			ans = max3(ans , pre[i - 1] + suf[ i + 1] , cum + suf[i + 1]);
		}
	}
	cout << ans << endl;
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
ll arr[100005];
ll pre[100005];
ll cnt ;
int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	int k = st.size() - 1;
	while(st[k] == '0') k--;
	ll ans = pre[k];
	ll ans2 = arr[k + 1];
	k--;
	for(int i = 0 ; i < k ; i++){
		cnt += (st[i] == '0');
	}
	while(st[k] == '0') k--;
	ll tot = k + 1 - cnt;
	for(int i = 0 ; i < k ; i++){
		v.pb(arr[i + 1]);
	}
	sort(all(v));
	reverse(all(v));
	for(auto x : v){
		if(tot == 0) break;
		ans2 += x;
		tot --;
	}
	cout << max(ans , ans2) << endl;
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
ll arr[100005];
ll pre[100005];
ll cnt ;
int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	int k = st.size() - 1;
	while(st[k] == '0') k--;
	ll ans = pre[k];
	ll ans2 = arr[k + 1];
	for(int i = 0 ; i < k ; i++){
		if(st[i] == 1){
			ans2 += arr[i + 1];
		}
	}
	cout << max(ans , ans2) << endl;
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
ll arr[100005];
ll pre[100005] , suf[100005];
ll cnt , ans ;
int main()
{
	int n;
	cin >> n;
	for(int i = 0 ; i < n; i++) cin >> arr[i];
	pre[0] = arr[0];
	for(int i = 1; i < n; i++){
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	suf[n] = 0;
	for(int i = n - 1; i >= 0 ; i--){
		if(st[i] == '1') {
			suf[i] = suf[i + 1] + arr[i];
		}
	}
	ll cum = 0;
	for(int i = 0; i < n ; i ++){
		if(st[i] == '1'){
			cum += arr[i];
		}
		if(st[i] == '0') ans = max(ans , cum + suf[i + 1]);
		else {
			ans = max3(ans , pre[i - 1] + suf[ i + 1] , cum + suf[i + 1]);
		}
	}
	cout << ans << endl;
	return 0;
}


/*****************Bismillahir Rahmanir Rahim***********************/

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

/************************Start Of Template*************************/

/******** Mahin Shefat - GhOstMan *************/

#define     pb              push_back
#define     popb            pop_back
#define     ll              long long int
#define     ull             unsigned long long int
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     MOD             1000000009
#define     f               first
#define     s               second
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     mp              make_pair
#define     check           printf("check")
#define     sqr(x)          (x)*(x)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     pii             pair<int,int>
#define     Pair            pair<ll,ll>
#define     MAX             1000000000000000000
#define     MIN             -100000000000014
#define     Fread           freopen("input.txt","r",stdin);
#define     Fwrite          freopen("output.txt","w",stdout);

/************************End Of Template*************************/
string st;
std::vector<ll> v;
int arr[100005];
int pre[100005];
int cnt ;
int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	cin >> st;
	int k = st.size() - 1;
	while(st[k] == '0') k--;
	ll ans = pre[k];
	ll ans2 = arr[k + 1];
	k--;
	while(st[k] == '0') k--;
	for(int i = 0 ; i < k ; i++){
		cnt += (st[i] == '0');
	}
	ll tot = k + 1 - cnt;
	for(int i = 0 ; i < k ; i++){
		v.pb(arr[i + 1]);
	}
	sort(all(v));
	reverse(all(v));
	for(auto x : v){
		if(tot == 0) break;
		ans2 += x;
		tot --;
	}
	cout << max(ans , ans2) << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define SZ(x) ((int)(x).size())
#define le length()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fastIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define c(a) cout << a << "\n";
#define endl '\n'
const int MOD = 1e9 + 7;
const int N = 3e6 + 6;
#define pi 3.141592653589793
// int v[N];
// void add(int idx, int val)
// {
//     while (idx <= N)
//     {
//         v[idx] += val;
//         idx += idx & -idx;
//     }
// }

// int sum(int idx)
// {
//     int ans = 0;
//     while (idx > 0)
//     {
//         ans += v[idx];
//         idx -= idx & -idx;
//     }
//     return ans;
// }
// vector<pair<ll, ll>> v;
// bool comp(pair<ll, ll> a, pair<ll, ll> b)
// {
//     if (a.fi < b.fi)
//         return true;
//     else if (a.fi == b.fi)
//         return (a.se > b.se);
//     else if (a.fi > b.fi)
//         return false;
// }
int main()
{
    fastIO;
    ll n;
    cin >> n;
    ll a[n];
    rep(i, n) cin >> a[i];
    string s;
    cin >> s;
    ll pref[n];
    pref[0] = a[0];
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1] + a[i];

    ll ans = 0;
    rep(i, n)
    {
        if (s[i] == '1')
            ans += a[i];
    }
    ll sum = 0;
    for (int i = n - 1; i > 0; i--)
    {
        if (s[i] == '1')
        {

            ans = max(ans, sum + pref[i - 1]);
            sum += a[i];
        }
       // cout << ans << " ";
    }
    c(ans)

        return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
//#define ll unsigned long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define le length()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repo(i, n) for (ll i = 1; i <= n; i++)
#define foreach(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define fastIO                        \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);                    \
	cout.tie(NULL)
#define eps 1e-6
#define pi 3.141592653589793
#define c(a) cout << a << "\n";
#define ao(a)        \
	for (auto i : a) \
		cout << i << " ";
#define endl '\n'
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int N = 1e5 + 6;

ll powmod(ll a, ll b, ll mod)
{
	ll res = 1;
	a %= mod;
	for (; b; b >>= 1)
	{
		if (b & 1)
			res = (res * a) % mod;
		a = (a * a) % mod;
	}
	return res;
}

ll gcd(ll a, ll b)
{
	while (b > 0)
	{
		ll t = a % b;
		a = b, b = t;
	}
	return a;
}

ll lcm(ll a, ll b)
{
	return (a / gcd(a, b)) * b;
}

ll is_prime(ll n)
{
	if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
		return 0;
	for (ll i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
		if (n % i == 0)
			return 0;
	return 1;
}
ll modI(ll a, ll m)
{
	ll m0 = m, y = 0, x = 1;
	if (m == 1)
		return 0;
	while (a > 1)
	{
		ll q = a / m;
		ll t = m;
		m = a % m;
		a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	if (x < 0)
		x += m0;
	return x;
}
// ll powbin(ll base, ll p)
// {
// 	ll mul;

// 	if (p == 1) mul = base;
// 	else {
// 		mul = (ll)powbin(base, p / 2);
// 		mul = (mul * mul) % MOD;
// 		if (p % 2) mul *= base;
// 	}

// 	return mul % MOD;
// }

// ll combination(ll n, ll r)
// {
// 	ll num = 1, den = 1;

// 	for (ll i = n; i >= n - r + 1; i--) {
// 		num = (num * i) % MOD;
// 	}
// 	for (ll i = r; i >= 1; i--) {
// 		den = (den * i) % MOD;
// 	}
// 	return (num * powbin(den, MOD - 2)) % MOD;
// }

// int v[(1<<20)];

// void init(void){
//     int i;
//     rep(i,(1<<20)) v[i] = 0;
// }

// void add(int x, int val){
//     for(int i=x;i<(1<<20);i=((i)|(i+1))) v[i] += val;
// }

// int sum(int x){
//     ll ans = 0;
//     for(int i=x;i>0;i=((i)&(i-1))) ans += v[i-1];
//     return ans;
// }
// long long get(vector<long long> &f, int pos) {
// 	long long res = 0;
// 	for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
// 		res += f[pos];
// 	return res;
// }

// void upd(vector<long long> &f, int pos, int val) {
// 	for (; pos < int(f.size()); pos |= pos + 1) {
// 		f[pos] += val;
// 	}
// }
/*************************************************************************************************************/
// int tree[N],a[N];
// ll build(ll l,ll r,ll node){
//     if(r<l) return N;
//     if(l==r) {tree[node]=a[l];return N;}
//     ll mid=(l+r)/2;
//     build(l,mid,2*node+1);
//     build(mid+1,r,2*node+2);
//     tree[node] = min(tree[2*node+1],tree[2*node+2]);

// }
// void update(ll l,ll r,ll node,ll val,ll pos){
//     if(r<l) return;
//     if(l==r && l==pos) {tree[node]=val;return;}
//     ll mid=(l+r)/2;
//     if(pos<=mid) update(l,mid,2*node+1,val,pos);
//     else update(mid+1,r,2*node+2,val,pos);
//     tree[node]=min(tree[2*node+1],tree[2*node+2]);
// }
// ll sum(ll l,ll r,ll x,ll y,ll node){
//     //if(x<l || y>r) return 0;
//     if(r<x || y<l) return N;
//     //if(l==r) return tree[node];
//    // if(y<x || r<l) return 0;
//     if(x<=l && r<=y) return tree[node];
//     ll mid=(l+r)/2;
//     return min(sum(l,mid,x,y,2*node+1),sum(mid+1,r,x,y,2*node+2));
// }
// bool prime[100005];
// void SieveOfEratosthenes(int n)
// {

//     memset(prime, true, sizeof(prime));

//     for (int p=2; p*p<100005; p++)
//     {

//         if (prime[p] == true)
//         {

//             for (int i=p*p; i<100005; i += p)
//                 prime[i] = false;
//         }
//     }
// }
int main()
{
	ll n;
	cin >> n;
	ll a[n];
	rep(i, n) cin >> a[i];
	string s;
	cin >> s;
	ll sum = 0;
	rep(i, s.le)
	{
		if (s[i] == '1')
			sum += a[i];
	}
	ll ans = 0;
	for (int i = 1; i < n; i++)
		a[i] += a[i - 1];
	for (int i = s.le - 1; i >= 0; i--)
	{
		if (s[i] == '1')
		{
			if (i - 1 >= 0)
				ans = max(sum, a[i - 1]);
			else
				ans = sum;
			break;
		}
	}
	c(ans) return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define SZ(x) ((int)(x).size())
#define le length()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fastIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define c(a) cout << a << "\n";
#define endl '\n'
const int MOD = 1e9 + 7;
const int N = 3e6 + 6;
#define pi 3.141592653589793
// int v[N];
// void add(int idx, int val)
// {
//     while (idx <= N)
//     {
//         v[idx] += val;
//         idx += idx & -idx;
//     }
// }

// int sum(int idx)
// {
//     int ans = 0;
//     while (idx > 0)
//     {
//         ans += v[idx];
//         idx -= idx & -idx;
//     }
//     return ans;
// }
// vector<pair<ll, ll>> v;
// bool comp(pair<ll, ll> a, pair<ll, ll> b)
// {
//     if (a.fi < b.fi)
//         return true;
//     else if (a.fi == b.fi)
//         return (a.se > b.se);
//     else if (a.fi > b.fi)
//         return false;
// }
int main()
{
    fastIO;
    ll n;
    cin >> n;
    ll a[n];
    rep(i, n) cin >> a[i];
    string s;
    cin >> s;
    ll pref[n];
    pref[0] = a[0];
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1] + a[i];

    ll ans = 0;
    rep(i, n)
    {
        if (s[i] == '1')
            ans += a[i];
    }
    ll sum = 0;
    // c(ans)
    for (int i = 1; i < n; i++)
    {
        if (s[i] == '1')
        {
            ans = max(ans, sum + pref[i - 1]);
            sum += a[i];
        }
        // cout << ans << " ";
    }
    c(ans)

        return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<vector>
#include<queue>
#include<numeric>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<sstream>

#if DEBUG && !ONLINE_JUDGE
	#include "debugger.cpp"
#else
    #define debug(args...)
	#define printMatrix(args...)
    #define printArray(args...)
    #define printSegmentTree(args...)
#endif

#define pb push_back
#define mp make_pair
#define so sizeof
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define ord(x) x - 'a'

#define vvc vector<vector<char>>
#define vvb vector<vector<bool>>
#define vvi vector<vector<int>>
#define vi vector<int>

#define lli long long int
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
const int MAXN = 8;
const int MAXR = 1;
const int MAXC = 1;
const int MAXBITS = 64;
const int INF = (int)1e9;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int dx8[] = {-1,-1,-1,0,0,1,1,1};
int dy8[] = {-1,0,1,-1,1,-1,0,1};
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
void l(){
	debug("****************************************");
}
int arr[MAXN];

int solve(vi &v, string s, int n){

	vi v2(n), v3(n);
	v2[0] = v[0];
	v3[0] = s[0] == '1' ? v[0] : 0;

	int x,y;
	int lc = s[0] == '1' ? 0 : -1;
	debug("V:", v);
	debug("S:", s);
	FOR(i,1,n){
		v2[i] = v2[i-1] + v[i];
		v3[i] = v3[i-1] + (s[i] == '1' ? v[i] : 0);
		if(s[i] == '1'){
			lc = i;
		}
	}
	debug("LC:", lc);
	if(lc == -1){
		return 0;
	}
	debug("V:", v);
	debug("S:", s);
	debug("V2:", v2);
	debug("V3:", v3);

	int lp,rp,cans,ans = 0;
	FOR(i,0,lc+1){
		//It means s[i] has been set as 0. If s[i] was 1,
		//we can count v2[i-1] (i.e. sum of all less than i)
		//and v3[lc] - v3[i]	(i.e. sum of only those which are equal to 1 and greater than i)

		lp = 0;
		rp = 0;
		if(i>0){
			if(s[i] == '1'){
				lp = v2[i-1];
			}else{
				lp = v3[i-1];
			}
		}

		rp = v3[lc] - v3[i];
		cans = lp + rp;
		debug("I:", i, "LP:", lp, "RP:", rp, "CANS:", cans, "ANS:", ans);
		ans = max(ans, cans);
	}
	debug("If None of them is set as zero ans is:", v3[lc]);
	ans = max(ans, v3[lc]);
	return ans;
}

int main(void){
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        //freopen("output.txt", "w+", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;vi v(n);FOR(i,0,n)cin>>v[i];
	string s;cin>>s;
    int ans = solve(v, s, n);
    cout<<ans;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<vector>
#include<queue>
#include<numeric>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<sstream>

#if DEBUG && !ONLINE_JUDGE
	#include "debugger.cpp"
#else
    #define debug(args...)
	#define printMatrix(args...)
    #define printArray(args...)
    #define printSegmentTree(args...)
#endif

#define pb push_back
#define mp make_pair
#define so sizeof
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define ord(x) x - 'a'

#define vvc vector<vector<char>>
#define vvb vector<vector<bool>>
#define vvi vector<vector<int>>
#define vi vector<int>

#define lli long long int
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
const int MAXN = 8;
const int MAXR = 1;
const int MAXC = 1;
const int MAXBITS = 64;
const int INF = (int)1e9;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int dx8[] = {-1,-1,-1,0,0,1,1,1};
int dy8[] = {-1,0,1,-1,1,-1,0,1};
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
void l(){
	debug("****************************************");
}
int arr[MAXN];

int main(void){
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        //freopen("output.txt", "w+", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;vi v(n);FOR(i,0,n)cin>>v[i];string s;cin>>s;
    debug("V:", v);
    debug("S:", s);
    int a1 = 0;
    int lc = -1;
    int x;
    FOR(i,0,n){
    	x = s[i] == '1' ? v[i] : 0;
    	a1+=x;
    	if(s[i] == '1'){
    		lc = i;
    	}
    }
    int a2 = 0;
    FOR(i,0,lc){
    	a2+=v[i];
    }
    debug("A1:", a1, "A2:", a2);
    int ans = max(a1, a2);
    cout<<ans;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<vector>
#include<queue>
#include<numeric>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<sstream>

#if DEBUG && !ONLINE_JUDGE
	#include "debugger.cpp"
#else
    #define debug(args...)
	#define printMatrix(args...)
    #define printArray(args...)
    #define printSegmentTree(args...)
#endif

#define pb push_back
#define mp make_pair
#define so sizeof
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define ord(x) x - 'a'

#define vvc vector<vector<char>>
#define vvb vector<vector<bool>>
#define vvi vector<vector<int>>
#define vi vector<int>

#define lli long long int
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
const int MAXN = 8;
const int MAXR = 1;
const int MAXC = 1;
const int MAXBITS = 64;
const int INF = (int)1e9;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int dx8[] = {-1,-1,-1,0,0,1,1,1};
int dy8[] = {-1,0,1,-1,1,-1,0,1};
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
void l(){
	debug("****************************************");
}
int arr[MAXN];

int solve(vi &v, string s){
	int n = v.size();
	vi v2(n), v3(n);
	v2[0] = v[0];
	v3[0] = s[0] == '1' ? v[0] : 0;

	int x,y,lc;
	FOR(i,1,n){
		v2[i] = v2[i-1] + v[i];
		v3[i] = v3[i-1] + (s[i] == '1' ? v[i] : 0);
		if(s[i] == '1'){
			lc = i;
		}
	}
	debug("V:", v);
	debug("S:", s);
	debug("V2:", v2);
	debug("V3:", v3);

	int lp,rp,cans,ans = 0;
	FOR(i,0,lc+1){
		//It means s[i] has been set as 0. If s[i] was 1,
		//we can count v2[i-1] (i.e. sum of all less than i)
		//and v3[lc] - v3[i]	(i.e. sum of only those which are equal to 1 and greater than i)

		lp = 0;
		rp = 0;
		if(i>0){
			if(s[i] == '1'){
				lp = v2[i-1];
			}else{
				lp = v3[i-1];
			}
		}

		rp = v3[lc] - v3[i];
		cans = lp + rp;
		debug("I:", i, "LP:", lp, "RP:", rp, "CANS:", cans, "ANS:", ans);
		ans = max(ans, cans);
	}
	debug("If None of them is set as zero ans is:", v3[lc]);
	ans = max(ans, v3[lc]);
	return ans;
}

int main(void){
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        //freopen("output.txt", "w+", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;vi v(n);FOR(i,0,n)cin>>v[i];string s;cin>>s;
    int ans = solve(v, s);
    cout<<ans;
	return 0;
}