#include <bits/stdc++.h>
using namespace std;
const int MaxN = 105;
using ll = long long;
int N;
string s;
ll a[MaxN];
ll D[MaxN][MaxN][MaxN];
void ReadData();
void Solve();
int main() {
  ReadData();
  Solve();
  cout << D[0][N - 1][0] << '\n';
  return 0;
}
void ReadData() {
  cin >> N;
  cin >> s;
  for (int i = 1; i <= N; ++i) {
    cin >> a[i];
    for (int j = 1; j < i; ++j) a[i] = max(a[i], a[i - j] + a[j]);
  }
}
void Solve() {
  for (int i = 0; i < N; ++i)
    for (int k = 0; k <= N; ++k) D[i][i][k] = a[1 + k];
  for (int i = 1; i < N; ++i)
    for (int k = 1; k <= N; ++k) D[i - 1][i][k] = a[k];
  for (int d = 1; d < N; ++d)
    for (int i = 0; i < N; ++i) {
      int j = i + d;
      for (int k = 0; k <= N - (j - i + 1); ++k) {
        D[i][j][k] = D[i + 1][j][0] + a[k + 1];
        for (int p = i; p <= j; ++p)
          if (s[i] == s[p + 1])
            D[i][j][k] = max(D[i][j][k], D[i + 1][p][0] + D[p + 1][j][k + 1]);
      }
    }
}
