#include <bits/stdc++.h>
using namespace std;
long long gain[105];
int len[105];
int val[105];
int M;
int N;
long long a[105];
char s[105];
long long DP[105][105][105];
bool vis[105][105][105];
long long solve(int l, int r, int x) {
  if (l > r) return 0;
  if (l == r) {
    return gain[x + len[l]];
  }
  if (vis[l][r][x]) {
    return DP[l][r][x];
  }
  long long& ret = DP[l][r][x];
  vis[l][r][x] = 1;
  ret = max(ret, solve(l, r - 1, 0) + gain[x + len[r]]);
  for (int m = l; m < r; ++m) {
    if (val[r] == val[m]) {
      ret = max(ret, solve(l, m, len[r] + x) + solve(m + 1, r - 1, 0));
    }
  }
  if (0) printf("DP[%d][%d][%d] = %lld\n", l, r, x, DP[l][r][x]);
  return ret;
}
int main() {
  cin >> N;
  cin >> (s + 1);
  for (int i = 1; i <= N; ++i) cin >> a[i];
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j <= i; ++j) {
      gain[i] = max(gain[i], gain[i - j] + a[j]);
    }
    if (0) printf("gain[%d] = %lld\n", i, gain[i]);
  }
  M = 1;
  len[M] = 1;
  val[M] = (s[M] == '0') ? 0 : 1;
  for (int i = 2; i <= N; ++i) {
    if (s[i] == s[i - 1]) {
      ++len[M];
    } else {
      ++M;
      ++len[M];
      val[M] = (s[i] == '0') ? 0 : 1;
    }
  }
  if (0)
    for (int i = 1; i <= M; ++i) {
      printf("len[%d] = %d\n", i, len[i]);
      printf("val[%d] = %d\n", i, val[i]);
    }
  printf("%lld\n", solve(1, M, 0));
}
