#include <bits/stdc++.h>
using namespace std;
int n, a[105] = {0};
char s[105];
int sum[105] = {0};
long long f[2][105][105][105] = {0}, ff[105][105] = {0};
int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + s[i] - '0';
  memset(f, -1, sizeof(f));
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '0') {
      f[0][i][i][1] = 0;
    } else {
      f[1][i][i][1] = 0;
    }
    f[0][i][i][0] = f[1][i][i][0] = ff[i][i] = a[1];
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j + i <= n; ++j) {
      int lim = max(i + 1 - (sum[j + i] - sum[j - 1]), sum[j + i] - sum[j - 1]);
      for (int k = 1; k <= lim; ++k) {
        for (int l = j; l < j + i; ++l) {
          int sig = s[l] - '0';
          long long &dp = f[sig][j][j + i][k];
          if (f[sig][l + 1][j + i][k - 1] >= 0)
            dp = max(dp, ff[j][l - 1] + f[sig][l + 1][j + i][k - 1]);
        }
        if (k == 1) {
          int sig = s[j + i] - '0';
          long long &dp = f[sig][j][j + i][1];
          dp = max(dp, ff[j][j + i - 1]);
        }
      }
      for (int k = 1; k <= lim; ++k) {
        if (f[0][j][j + i][k] >= 0)
          ff[j][j + i] = max(f[0][j][j + i][k] + a[k], ff[j][j + i]);
        if (f[1][j][j + i][k] >= 0)
          ff[j][j + i] = max(f[1][j][j + i][k] + a[k], ff[j][j + i]);
      }
    }
  }
  printf("%I64d\n", ff[1][n]);
  return 0;
}
