题目链接:http://codeforces.com/contest/1107/problem/E
#include <bits/stdc++.h>
#define pi acos(-1.0 )
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;//4e18 ~= 2^62
const int maxn =100 + 10;
const LL mod = 1e9+7;
string s;
LL a[maxn];
LL dp[maxn][maxn][maxn];
LL dfs(int l, int r, int k)
{
if(l>r) return 0;
if(l == r) return a[k+1];
if(dp[l][r][k] != -1) return dp[l][r][k];
dp[l][r][k] = dfs(l,r-1, 0) + a[k+1];
for(int i=l; i<r; i++){
if(s[i] == s[r]){
dp[l][r][k] = max(dp[l][r][k], dfs(l, i, k+1) + dfs(i+1, r-1, 0));
}
}
return dp[l][r][k];
}
int main()
{
int n;
scanf("%d", &n);
cin >> s;
for(int i=1; i<=n; i++){
scanf("%I64d", &a[i]);
}
memset(dp, -1, sizeof(dp));
printf("%I64d\n", dfs(0, n-1, 0));
}