#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
const int M = 1e6;
const long long oo = 1e18;
const int mod = 1e9 + 7;
long long a[110], dp[110][110][2], ans[110][110][110];
char s[110];
long long calc(int l, int r, int cnt) {
  if (l > r) return 0;
  if (l == r) return a[cnt];
  long long &res = ans[l][r][cnt];
  if (res != 0) return res;
  res = a[cnt] + calc(l + 1, r, 1);
  for (int i = l + 1; i <= r; i++) {
    if (s[i] == s[l])
      res = max(res, calc(l + 1, i - 1, 1) + calc(i, r, cnt + 1));
  }
  return res;
}
int main() {
  int n;
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    for (int j = 1; j <= i; j++) a[i] = max(a[i], a[i - j] + a[j]);
  }
  cout << calc(1, n, 1) << endl;
}
