#include <bits/stdc++.h>
using namespace std;
const int N = 105;
long long dp[N][N][N];
bool us[N][N][N];
int ar[N];
string str;
int n;
long long solve(int l, int r, int k) {
  if (r < l) return 0;
  if (l == r) return ar[k - 1];
  long long &ret = dp[l][r][k];
  if (us[l][r][k]) return ret;
  us[l][r][k] = true;
  ret = solve(l + 1, r, 1) + ar[k - 1];
  for (int t = l + 1; t <= r; t++) {
    if (str[t] != str[l]) continue;
    ret = max(ret, solve(l + 1, t - 1, 1) + solve(t, r, k + 1));
  }
  return ret;
}
int main() {
  scanf(" %d", &n);
  cin >> str;
  for (int i = 0; i < n; i++) scanf(" %d", &ar[i]);
  cout << solve(0, n - 1, 1) << endl;
  return 0;
}
