#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int res = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * f;
}
const int N = 105;
const int MOD = 1000000007;
template <typename T>
void chmin(T& a, T b) {
  if (a > b) a = b;
}
template <typename T>
void chmax(T& a, T b) {
  if (b > a) a = b;
}
struct Node {
  int x, y;
} t[N];
long long f[N], memo[N][N];
int a[N], n, top;
long long dp[N][N][N];
char s[N];
long long solve(int num, int tot) {
  if (memo[num][tot] != -1) return memo[num][tot];
  if (num == 1) return memo[num][tot] = a[tot];
  for (int i = 1; i <= tot - num + 1; ++i) {
    chmax(memo[num][tot], a[i] + solve(num - 1, tot - i));
  }
  return memo[num][tot];
}
long long get_f(int x) {
  long long ans = a[x];
  for (int i = 2; i <= x; ++i) chmax(ans, solve(i, x));
  return ans;
}
long long dfs(int l, int r, int num) {
  if (dp[l][r][num] != -1) return dp[l][r][num];
  if (l > r) return 0;
  if (l == r) return dp[l][r][num] = f[t[r].y + num];
  long long& v = dp[l][r][num];
  chmax(v, dfs(l, r - 1, 0) + f[t[r].y + num]);
  for (int i = l; i < r; ++i) {
    if (t[i].x == t[r].x) {
      chmax(v, dfs(l, i, t[r].y + num) + dfs(i + 1, r - 1, 0));
    }
  }
  return v;
}
int main() {
  n = read();
  scanf("%s", s + 1);
  memset(memo, -1, sizeof memo);
  memset(dp, -1, sizeof dp);
  int cur = -1, cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] - '0' != cur) {
      if (cnt) t[++top] = (Node){cur, cnt};
      cur = s[i] - '0';
      cnt = 1;
    } else {
      ++cnt;
    }
  }
  t[++top] = (Node){cur, cnt};
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) f[i] = get_f(i);
  printf("%lld", dfs(1, top, 0));
  return 0;
}
