#include <bits/stdc++.h>
using namespace std;
string s;
long long result[101][301][101][2], c[101], a[101], cd, del_all[101][101];
long long got[101][2];
void inp() {
  cin >> cd;
  cin >> s;
  s = '0' + s;
  for (long long i = 1; i <= cd; i++) cin >> a[i];
}
void dp_for_c() {
  for (long long i = 1; i <= cd; i++) {
    c[i] = a[i];
    for (long long e = i; e >= 1; e--) c[i] = max(c[i], c[i - e] + c[e]);
  }
  for (long long i = 1; i <= cd; i++) {
    got[i][1] = got[i - 1][1];
    got[i][0] = got[i - 1][0];
    if (s[i] == '0')
      got[i][0]++;
    else
      got[i][1]++;
  }
}
void do_it() {
  dp_for_c();
  for (long long e = 1; e <= cd; e++)
    for (long long i = 1; i <= cd; i++)
      for (long long f = 1; f <= cd; f++)
        result[i][i + e - 1][f][0] = result[i][i + e - 1][f][1] = -1e15;
  for (long long e = 1; e <= cd; e++)
    for (long long i = 1; i <= cd; i++)
      for (long long f = 1; f <= cd; f++) {
        long long en = i + e - 1;
        for (long long t = 0; t <= 1; t++) {
          int ok = 1;
          if (i + e - 1 > cd || got[en][t] - got[i - 1][t] < f) continue;
          for (int g = en; g >= i; g--) {
            if (en == 9 && i == 4 && f == 2 && t == 0) {
            }
            if (g + 1 <= en)
              result[i][en][f][t] = max(result[i][g][f][t] + del_all[g + 1][en],
                                        result[i][en][f][t]);
            if (g - 1 >= i)
              result[i][en][f][t] = max(
                  result[i][en][f][t], result[g][en][f][t] + del_all[i][g - 1]);
            if (s[g] - '0' != t || g - 1 < i) ok = 0;
            if (ok == 0) continue;
            int num = (en - g + 1);
            result[i][en][f][t] =
                max(result[i][en][f][t], result[i][g - 1][f - num][t]);
          }
          if (e == 1) {
            if (s[i] - '0' == t) result[i][en][f][t] = 0;
            del_all[i][en] = max(del_all[i][en], result[i][en][f][t] + c[f]);
            result[i][en][0][0] = max(result[i][en][0][0], del_all[i][en]);
            result[i][en][0][1] = max(result[i][en][0][1], del_all[i][en]);
            continue;
          }
          result[i][en][f][t] =
              max(result[i][en][f][t],
                  max(result[i][en - 1][f][t], result[i + 1][en][f][t]) + c[1]);
          if (en - 1 >= i + 1)
            result[i][en][f][t] = max(result[i][en][f][t],
                                      result[i + 1][en - 1][f][t] + 2 * c[1]);
          if (s[i] - '0' == t && f >= 1 && i + 1 <= en)
            result[i][en][f][t] =
                max(result[i][en][f][t], result[i + 1][en][f - 1][t]);
          if (s[i] - '0' == t && s[en] - '0' == t && f >= 2 && i + 1 <= en - 1)
            result[i][en][f][t] =
                max(result[i][en][f][t], result[i + 1][en - 1][f - 2][t]);
          if (s[en] - '0' == t && f >= 1 && i <= en - 1)
            result[i][en][f][t] =
                max(result[i][en][f][t], result[i][en - 1][f - 1][t]);
          del_all[i][en] = max(del_all[i][en], result[i][en][f][t] + c[f]);
          result[i][en][0][0] = max(result[i][en][0][0], del_all[i][en]);
          result[i][en][0][1] = max(result[i][en][0][1], del_all[i][en]);
        }
      }
  cout << del_all[1][cd] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  inp();
  do_it();
}
