#include <bits/stdc++.h>
namespace IO {
const int N = 1 << 20;
char buf[N], *l = buf, *r = buf;
inline char gc() {
  if (l == r) r = (l = buf) + fread(buf, 1, N, stdin);
  return l == r ? EOF : *(l++);
}
inline int read() {
  int x = 0, s = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-') s = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = gc();
  }
  return x * s;
}
inline int reads(char *s) {
  char *point = s;
  *point = gc();
  while (*point <= 32) *point = gc();
  while (*point > 32) *(++point) = gc();
  *point = '\0';
  return point - s;
}
}  // namespace IO
using namespace std;
using IO::read;
using IO::reads;
const int maxn = 1e2 + 10;
int val[maxn];
long long f[maxn][maxn][maxn];
char s[maxn];
inline long long dp(int l, int r, int len) {
  if (l > r) return 0;
  if (f[l][r][len]) return f[l][r][len];
  if (l == r) return f[l][r][len] = val[len + 1];
  f[l][r][len] = dp(l, r - 1, 0) + val[len + 1];
  for (int k = l; k < r; k++)
    if (s[k] == s[r])
      f[l][r][len] = max(f[l][r][len], dp(l, k, len + 1) + dp(k + 1, r - 1, 0));
  return f[l][r][len];
}
int main() {
  int n = read();
  reads(s + 1);
  for (int i = 1; i <= n; i++) val[i] = read();
  for (int i = 1; i <= n; i++) {
    int ed = i >> 1;
    for (int j = 1; j <= ed; j++) val[i] = max(val[i], val[j] + val[i - j]);
  }
  f[1][n][0] = dp(1, n - 1, 0) + val[1];
  for (int k = 1; k < n; k++)
    if (s[k] == s[n])
      f[1][n][0] = max(f[1][n][0], dp(1, k, 1) + dp(k + 1, n - 1, 0));
  printf("%lld\n", f[1][n][0]);
  return 0;
}
