#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long t = 0, f = 0;
  register char v = getchar();
  while (v < '0') f |= (v == '-'), v = getchar();
  while (v >= '0') t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return f ? -t : t;
}
long long n, a[102], dp[102][102][102];
char s[102];
inline long long ask(register long long l, register long long r,
                     register long long k) {
  if (l > r) return 0;
  if (l == r) return a[k];
  if (~dp[l][r][k]) return dp[l][r][k];
  dp[l][r][k] = ask(l + 1, r, 1) + a[k];
  for (register long long i = l + 1; i <= r; ++i)
    if (s[i] == s[l])
      dp[l][r][k] = max(dp[l][r][k], ask(l + 1, i - 1, 1) + ask(i, r, k + 1));
  return dp[l][r][k];
}
signed main() {
  n = read();
  scanf("%s", s + 1);
  for (register long long i = 1; i <= n; ++i) {
    a[i] = read();
    for (register long long j = i - 1; j; --j)
      a[i] = max(a[i], a[j] + a[i - j]);
  }
  memset(dp, -1, sizeof(dp));
  printf("%lld", ask(1, n, 1));
}
