#include <bits/stdc++.h>
using namespace std;
const int nmax = 100;
const long long inf = 1LL << 60;
long long d[nmax + 1][nmax + 1];
long long d2[2][nmax + 1][nmax + 1];
int cst[nmax + 1];
string s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n >> s;
  s = '#' + s;
  for (int i = 1; i <= n; ++i) cin >> cst[i];
  for (int i = n; i > 0; --i) {
    for (int val = 0; val < 2; ++val)
      for (int j = i; j <= n; ++j)
        for (int k = 0; k <= n; ++k) d2[val][j][k] = -inf;
    d2[s[i] - '0'][i][1] = 0;
    for (int j = i; j <= n; ++j) {
      for (int k = i; k < j; ++k) {
        d[i][j] = max(d[i][j], d[i][k] + d[k + 1][j]);
      }
      for (int val = 0; val < 2; ++val) {
        for (int k = 1; k <= j - i + 1; ++k) {
          d[i][j] = max(d[i][j], d2[val][j][k] + cst[k]);
        }
      }
      if (j + 1 <= n) {
        d2[s[j + 1] - '0'][j + 1][1] =
            max(d2[s[j + 1] - '0'][j + 1][1], d[i][j]);
        for (int val = 0; val < 2; ++val) {
          for (int x = j + 1; x <= n; ++x) {
            for (int k = 1; k <= j - i + 1; ++k) {
              d2[val][x][k] = max(d2[val][x][k], d2[val][j][k] + d[j + 1][x]);
            }
          }
        }
        for (int k = 1; k <= j - i + 1; ++k) {
          d2[s[j + 1] - '0'][j + 1][k + 1] =
              max(d2[s[j + 1] - '0'][j + 1][k + 1], d2[s[j + 1] - '0'][j][k]);
        }
      }
    }
  }
  cout << d[1][n] << "\n";
  return 0;
}
