#include <bits/stdc++.h>
#define int long long
#define LIMIT 114514
using namespace std;
const int N=2e5+5;
int n,m,res,ans;
int a[25],f[N];
string st;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
for(int j=i; j<=LIMIT; j++)
f[j]=max(f[j],f[j-i]+a[i]);
}
cin>>m>>st;
st="#"+st;
for(int i=1; i<=m; i++){
if(st[i]=='a'&&st[i+1]=='b'&&st[i+2]=='c'){
i+=2;
res++;
}
else{
ans+=f[res];
res=0;
}
}
cout<<ans<<"\n";
return 0;
}