#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, a[N], sum[N];
long long f[N][N][N];
char s[N];
int main() {
  scanf("%d", &n);
  scanf(" %s", s + 1);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 2; i <= n; i++)
    for (int j = 1; j < i; j++) a[i] = max(a[i], a[j] + a[i - j]);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) sum[i] += (s[i] == s[j]);
  for (int i = n; i; i--) {
    for (int j = i; j <= n; j++) {
      for (int k = i; k < j; k++) {
        if (s[k] == s[j]) {
          for (int l = 0; l <= sum[j]; l++) {
            f[i][j][l] = max(f[i][j][l], f[i][k][l + 1] + f[k + 1][j - 1][0]);
          }
        }
      }
      for (int k = 0; k <= sum[j]; k++) {
        f[i][j][k] = max(f[i][j][k], f[i][j - 1][0] + a[k + 1]);
      }
    }
  }
  printf("%lld", f[1][n][0]);
  return 0;
}
