#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long A[201], S[201][201], B[201][201][2][201];
long long i, j, k, l, r, md, q, n, m, mx, mn, a, b, x, c, s;
string s1, s2;
long long kkk(long long l, long long r, long long c, long long cnt);
long long solve(long long l, long long r) {
  if (S[l][r] != -1) return S[l][r];
  if (l == r) return S[l][r] = A[0];
  if (l > r) return S[l][r] = 0;
  long long n = r - l + 1, x = -2000000000;
  for (long long i = n - 1; i >= 0; i--) {
    x = max<long long>(x, A[i] + kkk(l, r, 0, i + 1));
    x = max<long long>(x, A[i] + kkk(l, r, 1, i + 1));
  }
  return S[l][r] = x;
}
long long kkk(long long l, long long r, long long c, long long cnt) {
  if (B[l][r][c][cnt] != -1) return B[l][r][c][cnt];
  if (cnt == 0) return B[l][r][c][cnt] = solve(l, r);
  long long x = -2000000000;
  char b = c == 0 ? '0' : '1';
  for (long long i = l; i <= r; i++) {
    if (s1[i] == b)
      x = max<long long>(x, solve(l, i - 1) + kkk(i + 1, r, c, cnt - 1));
  }
  return B[l][r][c][cnt] = x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
  for (__typeof(201) i = (0) - ((0) > (201)); i != (201) - ((0) > (201));
       i += 1 - 2 * ((0) > (201)))
    for (__typeof(201) j = (0) - ((0) > (201)); j != (201) - ((0) > (201));
         j += 1 - 2 * ((0) > (201)))
      S[i][j] = -1;
  for (__typeof(201) i = (0) - ((0) > (201)); i != (201) - ((0) > (201));
       i += 1 - 2 * ((0) > (201)))
    for (__typeof(201) j = (0) - ((0) > (201)); j != (201) - ((0) > (201));
         j += 1 - 2 * ((0) > (201)))
      for (__typeof(2) k = (0) - ((0) > (2)); k != (2) - ((0) > (2));
           k += 1 - 2 * ((0) > (2)))
        for (__typeof(201) m = (0) - ((0) > (201)); m != (201) - ((0) > (201));
             m += 1 - 2 * ((0) > (201)))
          B[i][j][k][m] = -1;
  cin >> n >> s1;
  for (int i = 0; i < n; i++) cin >> A[i];
  cout << solve(0, n - 1);
  return 0;
}
