#include <bits/stdc++.h>
using namespace std;
const int N = 103, P = 2;
const long long INF = 2e18 + 18;
int n, cost[N];
long long ans[N][N], dp[P][N][N][N];
string st;
void solve() {
  cin >> n >> st;
  for (int i = 0; i < n; ++i) scanf("%d", &cost[i + 1]);
  fill(&dp[0][0][0][0], &dp[0][0][0][0] + P * N * N * N, -INF);
  for (int add = 0; add < n; ++add)
    for (int l = 0; l + add < n; ++l) {
      int r = l + add;
      if (l == r) {
        dp[0][l + 1][r][0] = 0;
        dp[1][l + 1][r][0] = 0;
      }
      for (int key = 0; key < 2; ++key) {
        int cur_cnt = 0;
        for (int i = l; i <= r; ++i) cur_cnt += (st[i] - '0' == key);
        for (int cnt = 1; cnt <= cur_cnt; ++cnt) {
          for (int mid = l; mid <= r; ++mid)
            if (st[mid] - '0' == key)
              dp[key][l][r][cnt] =
                  max(dp[key][l][r][cnt],
                      ans[l][mid - 1] + dp[key][mid + 1][r][cnt - 1]);
          ans[l][r] = max(ans[l][r], dp[key][l][r][cnt] + cost[cnt]);
        }
      }
      dp[0][l][r][0] = ans[l][r];
      dp[1][l][r][0] = ans[l][r];
    }
  cout << ans[0][n - 1];
}
int main() {
  solve();
  return 0;
}
