#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
char s[MAXN];
int val[MAXN], n;
long long f[MAXN][MAXN][MAXN];
int main() {
  memset(f, 0, sizeof(f));
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
  }
  for (int l = n; l >= 1; l--) {
    for (int r = l; r <= n; r++) {
      for (int k = 0; k < n; k++) {
        f[l][r][k] = max(f[l][r - 1][0] + val[k + 1], f[l][r][k]);
        for (int v = l; v < r; v++) {
          if (s[v - 1] == s[r - 1]) {
            f[l][r][k] = max(f[l][r][k], f[v + 1][r - 1][0] + f[l][v][k + 1]);
          }
        }
      }
    }
  }
  cout << f[1][n][0] << endl;
  return 0;
}
