#include <bits/stdc++.h>
using namespace std;
long long dp_arr[101][101][101];
long long dp(string s, long long *arr, long long start, long long end,
             long long p) {
  if (start > end) return 0;
  if (dp_arr[start][end][p] != -1) return dp_arr[start][end][p];
  long long max_v =
      arr[p - 1] + ((start > end) ? (0) : (dp(s, arr, start + 1, end, 1)));
  for (long long i = start + 1; i <= end; i++) {
    if (s[i] == s[start]) {
      long long res = 0;
      if (start + 1 <= (i - 1)) res += dp(s, arr, start + 1, i - 1, 1);
      if (i <= end) res += dp(s, arr, i, end, p + 1);
      if (res > max_v) max_v = res;
    }
  }
  dp_arr[start][end][p] = max_v;
  return max_v;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i <= n; i++) {
    for (long long j = 0; j <= n; j++) {
      for (long long k = 0; k <= n; k++) {
        dp_arr[i][j][k] = -1;
      }
    }
  }
  string s;
  cin >> s;
  long long arr[n];
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  cout << dp(s, arr, 0, n - 1, 1) << "\n";
  return 0;
}
