#include <bits/stdc++.h>
using namespace std;
int n;
int a[110];
long long g[110], f[110][110][110];
char s[110];
int main() {
  int L, R;
  scanf("%d", &n);
  gets(s);
  if (s[0] != '0') gets(s);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &g[i]);
    for (int j = 1; j < i; j++) g[i] = max(g[i], g[j] + g[i - j]);
  }
  for (int i = 1; i <= n; i++) a[i] = s[i - 1] - 48;
  for (int t = 0; t < n; t++)
    for (int i = 1; i <= n - t; i++)
      for (int k = 1; k <= i; k++) {
        f[i][i + t][k] = g[k] + f[i + 1][i + t][1];
        for (int j = 1; j <= t; j++)
          if (a[i] == a[i + j])
            f[i][i + t][k] = max(f[i][i + t][k], f[i + 1][i + j - 1][1] +
                                                     f[i + j][i + t][k + 1]);
      }
  printf("%I64d\n", f[1][n][1]);
  return 0;
}
