#include <bits/stdc++.h>
using namespace std;
int n;
char s[110];
long long a[110], dp[110][110][110];
long long f(int l, int r, int k) {
  if (dp[l][r][k] != -1) return dp[l][r][k];
  if (l > r) return dp[l][r][k] = 0;
  long long ans = -1;
  ans = max(ans, a[k] + f(l + 1, r, 1));
  for (int i = l + 1; i <= r; i++) {
    if (s[i] == s[l]) {
      ans = max(ans, f(l + 1, i - 1, 1) + f(i, r, k + 1));
    }
  }
  return dp[l][r][k] = ans;
}
int main(void) {
  scanf("%d %s", &n, s);
  int i;
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", f(0, n - 1, 1));
  return 0;
}
