#include <bits/stdc++.h>
using namespace std;
const int MAX = 103;
int n;
char s[MAX];
int a[MAX];
long long dp[MAX][MAX][MAX];
long long solve(int l, int r, int prefix) {
  if (l > r) return 0;
  if (l == r) return a[prefix];
  long long &answer = dp[l][r][prefix];
  if (answer != 0) return answer;
  answer = a[prefix] + solve(l + 1, r, 1);
  for (int i = l + 1; i <= r; ++i) {
    if (s[l] == s[i])
      answer = max(answer, solve(l + 1, i - 1, 1) + solve(i, r, prefix + 1));
  }
  return answer;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  cout << solve(0, n - 1, 1) << endl;
  return 0;
}
