#include <bits/stdc++.h>
using namespace std;
const int maxn = 111;
vector<long long> v;
string s;
long long dp[maxn][maxn][maxn];
long long solve(int start, int end, int pre) {
  if (end <= start) return 0;
  if (end - start == 1) return v[pre];
  long long &ans = dp[start][end][pre];
  if (ans) return ans;
  ans = v[pre] + solve(start + 1, end, 1);
  for (int i = start + 1; i < end; ++i) {
    if (s[i] == s[start])
      ans = max(ans, solve(start + 1, i, 1) + solve(i, end, pre + 1));
  }
  return ans;
}
int main(int argc, const char *argv[]) {
  v.assign(111, 0);
  int n;
  cin >> n;
  cin >> s;
  for (int i = 1; i <= n; ++i) cin >> v[i];
  cout << solve(0, n, 1) << endl;
  return 0;
}
