#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e2 + 5, M = 6 * N + 5;
int n;
char second[N];
long long cst[N], a[N];
long long pre(int ba2e) {
  if (!ba2e) return 0;
  long long &ret = cst[ba2e];
  if (~ret) return ret;
  ret = a[ba2e];
  for (int i = 1; i <= ba2e; ++i) ret = max(ret, pre(i) + pre(ba2e - i));
  return ret;
}
long long dp[N][N][N];
long long solve(int l, int r, int cnt) {
  if (l > r) return 0;
  if (l == r) return cst[cnt + 1];
  long long &ret = dp[l][r][cnt];
  if (~ret) return ret;
  ret = 0;
  ret = max(ret, solve(l + 1, r, 0) + cst[cnt + 1]);
  for (int i = l + 1; i <= r; ++i)
    if (second[l] == second[i])
      ret = max(ret, solve(l + 1, i - 1, 0) + solve(i, r, cnt + 1));
  return ret;
}
int main() {
  scanf("%d%s", &n, &second);
  for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
  memset(cst, -1, sizeof cst);
  pre(n);
  memset(dp, -1, sizeof dp);
  printf("%lld\n", solve(0, n - 1, 0));
  return 0;
}
