#include <bits/stdc++.h>
using namespace std;
long long int dp[105][105][105];
long long int a[105];
string s;
long long int n;
long long int go(long long int l, long long int r, long long int prefix) {
  if (l > r) {
    return 0;
  }
  if (dp[l][r][prefix] != -1) {
    return dp[l][r][prefix];
  }
  long long int ans = a[prefix + 1] + go(l + 1, r, 0);
  for (long long int i = l + 1; i <= r; i++) {
    if (s[i] == s[l]) {
      ans = max(ans, go(l + 1, i - 1, 0) + go(i, r, prefix + 1));
    }
  }
  return dp[l][r][prefix] = ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long int i = 2; i <= n; i++) {
    for (long long int j = 1; j < i; j++) {
      a[i] = max(a[i], a[j] + a[i - j]);
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << go(0, n - 1, 0) << "\n";
}
