#include <bits/stdc++.h>
using namespace std;
template <class T>
string tostring(T x) {
  ostringstream out;
  out << x;
  return out.str();
}
long long toint(string s) {
  istringstream in(s);
  long long x;
  in >> x;
  return x;
}
int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, -1, 1, -1, 1};
int kx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
int ky[8] = {2, -2, 2, -2, 1, -1, 1, -1};
string s;
int n;
vector<int> a;
long long memo2[111];
long long solve2(int cnt) {
  if (cnt == 0) return 0;
  long long &res = memo2[cnt];
  if (res != -1) return res;
  res = 0;
  for (int i = 1; i <= cnt; i++) res = max(res, a[i] + solve2(cnt - i));
  return res;
}
long long memo[111][111][111];
long long solve(int start, int end, int cnt) {
  if (start == end + 1) return a[cnt];
  long long &res = memo[start][end][cnt];
  if (res != -1) return res;
  res = a[cnt + 1] + solve(start + 1, end, 0);
  for (int i = start + 1; i <= end; i++)
    if (s[i] == s[start]) {
      res = max(res, solve(start + 1, i - 1, 0) + solve(i, end, cnt + 1));
    }
  return res;
}
void solveOne() {
  cin >> n >> s;
  a.resize(n + 1);
  a[0] = 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));
  cout << solve(0, n - 1, 0) << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  int T = 1;
  while (T--) {
    solveOne();
  }
  return 0;
}
