#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
int a[N];
char s[N];
long long f[N][N][N];
long long query(int u, int v, int w) {
  if (u == v) {
    return a[w];
  }
  if (u > v) {
    return 0;
  }
  if (f[u][v][w]) {
    return f[u][v][w];
  }
  long long rep = query(u + 1, v, 1) + a[w];
  for (int i = u + 1; i <= v; i++) {
    if (s[i] == s[u]) {
      rep = max(rep, query(u + 1, i - 1, 1) + query(i, v, w + 1));
    }
  }
  return f[u][v][w] = rep;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  printf("%lld\n", query(1, n, 1));
  return 0;
}
