#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char a = getchar();
  for (; a > '9' || a < '0'; a = getchar())
    if (a == '-') f = -1;
  for (; a <= '9' && a >= '0'; a = getchar()) x = x * 10 + a - '0';
  return x * f;
}
const int N = 100 + 5;
int n;
char s[N];
int a[N];
long long dp[N][N][N];
long long dfs(int l, int r, int sm) {
  if (l > r) return 0;
  if (l == r) return a[sm + 1];
  if (dp[l][r][sm] != -1) return dp[l][r][sm];
  long long res = 0;
  res = dfs(l + 1, r, 0) + a[sm + 1];
  for (register int mid = l + 1; mid <= r; mid++)
    if (s[l] == s[mid])
      res = max(res, dfs(l + 1, mid - 1, 0) + dfs(mid, r, sm + 1));
  return dp[l][r][sm] = res;
}
signed main() {
  n = read();
  scanf("%s", s + 1);
  for (register int i = 1; i <= n; i++) a[i] = read();
  memset(dp, -1, sizeof(dp));
  cout << dfs(1, n, 0) << endl;
  return 0;
}
