#include <bits/stdc++.h>
using namespace std;
long long dp[101][101][101];
string st;
vector<int> ar;
long long solve(int l, int r, int match) {
  if (l > r) return 0;
  if (l == r) {
    return ar[match];
  }
  if (dp[l][r][match] != -1) return dp[l][r][match];
  long long ans = ar[match + 1] + solve(l + 1, r, 0);
  for (auto i = (l + 1); i < r; i++) {
    if (st[i] == st[l]) {
      ans = max(ans, solve(l + 1, i, 0) + solve(i, r, match + 1));
    }
  }
  return dp[l][r][match] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  int n;
  cin >> n;
  ar.resize(n + 1);
  cin >> st;
  memset(dp, -1, sizeof dp);
  for (auto i = (1); i < n + 1; i++) cin >> ar[i];
  cout << solve(0, n, 0) << endl;
}
