#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline int read() {
  int x = 0, f = 1;
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch & 15);
    ch = getchar();
  }
  if (f)
    return x;
  else
    return -x;
}
const int N = 110;
int n;
long long a[N];
char S[N];
long long f[N][N][N];
long long dp(int l, int r, int k) {
  if (l > r) return 0;
  long long &ret = f[l][r][k];
  if (~ret) return ret;
  ret = a[k] + dp(l + 1, r, 1);
  for (int i = l + 1; i <= r; i++)
    if (S[l] == S[i]) ret = max(ret, dp(l + 1, i - 1, 1) + dp(i, r, k + 1));
  return ret;
}
int main() {
  n = read();
  scanf("%s", S + 1);
  for (int i = 1; i <= n; i++) a[i] = 1ll * read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++) a[i] = max(a[i], a[i - j] + a[j]);
  memset(f, -1, sizeof(f));
  printf("%I64d\n", dp(1, n, 1));
  return 0;
}
