#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const double EPS = 1e-6;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
int oo = 1e9;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int n;
long long t[105];
char s[105];
int pre[105];
long long dp[105][105][105];
long long solve(int l, int r, int cnt) {
  if (l > r) return 0;
  if (l == r) return t[cnt];
  long long &ret = dp[l][r][cnt];
  if (ret) return ret;
  ret = t[cnt] + solve(l + 1, r, 1);
  for (int i = l + 1; i <= r; i++)
    if (s[l] == s[i])
      ret = max(ret, solve(l + 1, i - 1, 1) + solve(i, r, cnt + 1));
  return ret;
}
int main() {
  scanf("%d%s", &n, s);
  for (int i = 1; i <= n; i++) scanf("%lld", &t[i]);
  cout << solve(0, n - 1, 1);
}
