#include <bits/stdc++.h>
using namespace std;
long long f[109][109][109];
int n, va[109];
char sa[109];
void maxc(long long &a, long long b) { a = max(a, b); }
int main() {
  scanf("%d%s", &n, sa + 1);
  memset(f, 0, sizeof f);
  for (int i = (1), ilim = (n); i <= ilim; i++) scanf("%d", &va[i]);
  for (int i = (n), ilim = (1); i >= ilim; i--)
    for (int j = (i), jlim = (n); j <= jlim; j++) {
      for (int k = (0), klim = (n - 1); k <= klim; k++)
        f[i][j][k] = va[k + 1] + f[i + 1][j][0];
      for (int k = (0), klim = (n - 1); k <= klim; k++)
        for (int z = (i), zlim = (j - 1); z <= zlim; z++)
          if (sa[i] == sa[z + 1])
            maxc(f[i][j][k], f[i + 1][z][0] + f[z + 1][j][k + 1]);
    }
  printf("%lld\n", f[1][n][0]);
  return 0;
}
