#include <bits/stdc++.h>
using namespace std;
int i, j, m, n, t, tt, k;
long long w[105], inf, f[105][105][105], ans, a[105];
char s[105];
long long getmax(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
int main() {
  inf = 1000000000000000000;
  scanf("%d", &n);
  scanf("%s", s);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (i = 1; i <= n; i++) {
    w[i] = a[i];
    for (j = 1; j <= i - 1; j++) {
      w[i] = getmax(w[i], a[j] + w[i - j]);
    }
  }
  for (i = 1; i <= n; i++) {
    for (k = 0; k <= n; k++) {
      f[0][i][k] = w[k];
    }
    for (k = 0; k <= n - 1; k++) {
      f[1][i][k] = w[k + 1];
    }
  }
  for (i = 2; i <= n; i++) {
    for (j = 1; j <= n - i + 1; j++) {
      for (k = 0; k <= n - 1; k++) {
        f[i][j][k] = f[i - 1][j + 1][0] + w[k + 1];
        for (t = 1; t <= i - 1; t++) {
          if (s[j - 1] == s[j + t - 1]) {
            f[i][j][k] =
                getmax(f[i][j][k], f[i - t][j + t][k + 1] + f[t - 1][j + 1][0]);
          }
        }
      }
    }
  }
  ans = f[n][1][0];
  printf("%lld\n", ans);
  return 0;
}
