#include <bits/stdc++.h>
const int MAX = 1e6 + 3;
const long long INF = (long long)1e18 + 2;
const double PI = acos(-1);
using namespace std;
int n;
long long dp[102] = {0};
string s;
vector<int> a;
long long memo[102][102][102];
long long solve(int i, int j, int prefix) {
  if (i > j) return 0;
  long long &ans = memo[i][j][prefix];
  if (ans != -1) return ans;
  char start = s[i];
  int len = 1, k = i;
  while (k + 1 <= j && s[k] == s[k + 1]) {
    len++;
    k++;
  }
  long long best = dp[len + prefix] + solve(i + len, j, 0);
  for (k = i + len; k <= j; k++) {
    if (s[k] == start) {
      best = max(best, solve(i + len, k - 1, 0) + solve(k, j, len + prefix));
    }
  }
  ans = best;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  cin >> s;
  a.resize(n + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];
  dp[1] = a[1];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      dp[i] = max(dp[i], a[i - j] + dp[j]);
    }
  }
  memset(memo, -1, sizeof memo);
  cout << solve(0, n - 1, 0) << endl;
  return 0;
}
