#include <bits/stdc++.h>
using namespace std;
int n;
char s[110];
long long a[110];
long long b[110];
long long dp[110][110][110];
long long dfs(int l, int r, int len) {
  if (dp[l][r][len] != -1) return dp[l][r][len];
  if (l == r) {
    dp[l][r][len] = a[b[r] + len];
    return dp[l][r][len];
  }
  long long ans = dfs(l, r - 1, 0) + a[len + b[r]];
  for (int i = r - 2; i >= l; i -= 2) {
    ans = max(ans, dfs(l, i, len + b[r]) + dfs(i + 1, r - 1, 0));
  }
  dp[l][r][len] = ans;
  return ans;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int k = 1; k <= n; k++) {
    for (int j = 1; j <= n; j++) {
      for (int i = 1; i <= n; i++) {
        if (i + j > n) break;
        a[i + j] = max(a[i + j], a[i] + a[j]);
      }
    }
  }
  int len = 0;
  int tmp = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (i != 1 && s[i] != s[i - 1]) {
      b[++len] = tmp;
      tmp = 1;
    } else {
      tmp++;
    }
  }
  n = len;
  cout << dfs(1, len, 0) << endl;
  return 0;
}
