#include <bits/stdc++.h>
using namespace std;
const long long N = 105;
long long n;
long long dp[N][N][N], f[N], a[N];
string s;
long long Dp(long long l, long long r, long long k) {
  if (l > r) {
    return 0;
  }
  if (~dp[l][r][k]) {
    return dp[l][r][k];
  }
  long long &res = dp[l][r][k];
  long long rm = r;
  while (rm > l && s[rm - 1] == s[rm]) {
    --rm;
  }
  res = max(res, Dp(l, rm - 1, 0) + f[r - rm + 1 + k]);
  for (long long i = l; i < r; ++i) {
    if (s[i] == s[r]) {
      res = max(res, Dp(l, i, k + 1) + Dp(i + 1, r - 1, 0));
    }
  }
  return res;
}
signed main() {
  cin >> n;
  cin >> s;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  f[1] = a[1];
  for (long long i = 2; i <= n; ++i) {
    for (long long j = 1; j <= i; ++j) {
      f[i] = max(f[i], f[i - j] + a[j]);
    }
  }
  memset(dp, -1, sizeof dp);
  cout << Dp(0, n - 1, 0) << endl;
  return 0;
}
