#include <bits/stdc++.h>
using namespace std;
using ll = long long;
char str[105];
ll best[105];
ll memo[105][105][105];
ll dp(int l, int r, int p) {
  if (l >= r) return 0;
  if (memo[l][r][p] != -1) return memo[l][r][p];
  int ptr = l;
  while (ptr < r && str[ptr] == str[l]) ptr++;
  ll ret = best[p + ptr - l] + dp(ptr, r, 0);
  for (int i = ptr; i < r; ++i) {
    if (str[i] == str[l]) {
      ret = max(ret, dp(ptr, i, 0) + dp(i, r, p + ptr - l));
    }
  }
  return memo[l][r][p] = ret;
}
int main() {
  int n;
  cin >> n >> str;
  for (int i = 1; i <= n; ++i) {
    cin >> best[i];
    for (int j = 1; j < i; ++j) {
      best[i] = max(best[i], best[j] + best[i - j]);
    }
  }
  memset(memo, -1, sizeof memo);
  cout << dp(0, n, 0) << '\n';
}
