#include <bits/stdc++.h>
using namespace std;
long long f[110][110][110];
int a[110];
char s[110];
int read() {
  int x = 0;
  char c = getchar(), flag = '+';
  while (!isdigit(c)) flag = c, c = getchar();
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return flag == '-' ? -x : x;
}
long long solve(int l, int r, int k) {
  if (l > r) return 0;
  if (l == r) return a[k];
  if (~f[l][r][k]) return f[l][r][k];
  long long tmp = a[k] + solve(l + 1, r, 1);
  for (int i = l + 1; i <= r; i++) {
    if (s[i] == s[l])
      tmp = max(tmp, solve(l + 1, i - 1, 1) + solve(i, r, k + 1));
  }
  return f[l][r][k] = tmp;
}
int main() {
  int n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) a[i] = read();
  memset(f, -1, sizeof(f));
  cout << solve(1, n, 1);
  return 0;
}
