#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void cmax(int& x, const int& y) { x = x > y ? x : y; }
void cmax(ll& x, const ll& y) { x = x > y ? x : y; }
void cmin(int& x, const int& y) { x = x < y ? x : y; }
template <class T>
istream& operator>>(istream& in, vector<T>& V) {
  for (auto& x : V) in >> x;
  return in;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& V) {
  for (auto x : V) out << x << ' ';
  return out;
}
template <class T>
void sort(vector<T>& V) {
  sort(V.begin(), V.end());
}
template <class T>
void reverse(vector<T>& V) {
  reverse(V.begin(), V.end());
}
template <class T>
int SZ(const vector<T>& V) {
  return (int)V.size();
}
void debug() { cerr << "whxorz" << '\n'; }
const int N = 105;
int n;
char s[N];
ll a[N];
ll f[N][N][N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  cin >> s + 1;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 2; i <= n; i++)
    for (int j = 1; j < i; j++) cmax(a[i], a[j] + a[i - j]);
  for (int len = 1; len <= n; len++) {
    for (int i = 1, j = i + len - 1; j <= n; i++, j++) {
      for (int k = 0; k <= n; k++) {
        cmax(f[i][j][k], f[i + 1][j][0] + a[k + 1]);
        for (int p = i + 1; p <= j; p++)
          if (s[i] == s[p]) {
            cmax(f[i][j][k], f[i + 1][p - 1][0] + f[p][j][k + 1]);
          }
      }
    }
  }
  cout << f[1][n][0] << '\n';
  return 0;
}
