#include <bits/stdc++.h>
using namespace std;
long long n, i, j, a[1010], k, la, f[1010][1010], g[1010][1010];
char s[1010];
int main() {
  scanf("%lld", &n);
  scanf("%s", s + 1);
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (i = n; i; i--) {
    f[i][i] = a[1];
    memset(g, 172, sizeof(g));
    g[i][1] = 0;
    for (j = i + 1; j <= n; j++)
      if (s[j] == s[i])
        for (la = 1; la <= n; la++) {
          for (k = i; k < j; k++)
            if (s[k] == s[i])
              g[j][la] = max(g[j][la], g[k][la - 1] + f[k + 1][j - 1]);
          f[i][j] = max(f[i][j], g[j][la] + a[la]);
        }
    for (j = i + 1; j <= n; j++)
      for (k = i; k < j; k++) f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j]);
  }
  printf("%lld", f[1][n]);
}
