#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int inf = INT_MAX;
template <typename T>
void read(T &x) {
  x = 0;
  long long f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
char s[N];
long long f[N][N][N], n, a[N];
signed main() {
  read(n);
  scanf("%s", s + 1);
  for (long long i = 1; i <= n; i++) read(a[i]);
  for (long long i = 2; i <= n; i++)
    for (long long j = 1; j <= i - 1; j++) a[i] = max(a[i], a[j] + a[i - j]);
  for (long long l = 1; l <= n; l++)
    for (long long i = 1; i <= n; i++) {
      long long j = i + l - 1;
      if (j > n) break;
      for (long long k = 0; k <= n; k++) {
        f[i][j][k] = max(f[i][j][k], f[i + 1][j][0] + a[k + 1]);
        for (long long mid = i + 1; mid <= j; mid++)
          if (s[i] == s[mid])
            f[i][j][k] =
                max(f[i][j][k], f[i + 1][mid - 1][0] + f[mid][j][k + 1]);
      }
    }
  printf("%lld\n", f[1][n][0]);
  return 0;
}
