#include <bits/stdc++.h>
using namespace std;
int N;
string str;
int A[105];
long long C[105];
long long M[105];
bool vis[105][105][105];
bool val[105][105][105];
long long DP[105][105][105];
void dp(int i, int j, int k) {
  if (k < 0) return;
  if (vis[i][j][k]) return;
  vis[i][j][k] = 1;
  int ai = A[i];
  if (i == j) {
    if (k != 0 && k != ai) return;
    val[i][j][k] = 1;
    if (k == 0) DP[i][j][k] = M[ai];
    return;
  }
  if (k == 0) {
    val[i][j][k] = 1;
    for (int k2 = 1; k2 <= N; k2++) {
      dp(i, j, k2);
      if (val[i][j][k2]) DP[i][j][0] = max(DP[i][j][0], DP[i][j][k2] + M[k2]);
    }
    return;
  }
  for (int p = i + 2; p <= j; p += 2) {
    dp(i + 1, p - 1, 0);
    dp(p, j, k - ai);
    if (!val[p][j][k - ai]) continue;
    val[i][j][k] = 1;
    DP[i][j][k] = max(DP[i][j][k], DP[i + 1][p - 1][0] + DP[p][j][k - ai]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> str;
  for (int i = 1; i <= N; i++) cin >> C[i];
  int n = 0;
  int pr = -1;
  for (char ci : str) {
    int x = ci - '0';
    if (x != pr) {
      A[++n] = 1;
      pr = x;
    } else {
      A[n]++;
    }
  }
  if (n % 2 == 0) n++;
  M[1] = C[1];
  for (int i = 2; i <= N; i++) {
    M[i] = C[i];
    for (int j = 1; j <= i - 1; j++) M[i] = max(M[i], M[j] + M[i - j]);
  }
  dp(1, n, 0);
  cout << DP[1][n][0] << '\n';
  return 0;
}
