#include <bits/stdc++.h>
using namespace std;
int const MAX_N = 103;
int64_t ans[MAX_N][MAX_N];
int64_t DP[2][MAX_N][MAX_N][MAX_N];
int a[MAX_N];
string s;
int n;
int64_t getDP(int digit, int L, int R, int cnt);
int64_t getAns(int L, int R) {
  if (L > R) return 0;
  if (ans[L][R] != -1e18) return ans[L][R];
  int64_t &res = ans[L][R];
  res = 0;
  for (int64_t i = 1; i <= R - L + 1; i++)
    for (int64_t j = 0; j <= 1; j++) res = max(res, a[i] + getDP(j, L, R, i));
  return res;
}
int64_t getDP(int digit, int L, int R, int cnt) {
  if (cnt == 0) return getAns(L, R);
  if (DP[digit][L][R][cnt] != -1e18) return DP[digit][L][R][cnt];
  int64_t &res = DP[digit][L][R][cnt];
  for (int64_t i = L; i <= R; i++)
    if (digit == (s[i] - '0'))
      res = max(res, getAns(L, i - 1) + getDP(digit, i + 1, R, cnt - 1));
  return res;
}
int main() {
  cin >> n >> s;
  for (int64_t i = 1; i <= n; i++) cin >> a[i];
  for (int64_t i = 0; i < MAX_N; i++)
    for (int64_t j = 0; j < MAX_N; j++) ans[i][j] = -1e18;
  for (int64_t i = 0; i < 2; i++)
    for (int64_t j = 0; j < MAX_N; j++)
      for (int64_t k = 0; k < MAX_N; k++)
        for (int64_t l = 0; l < MAX_N; l++) DP[i][j][k][l] = -1e18;
  cout << getAns(0, n - 1);
}
