#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  bool f = true;
  char c = getchar();
  for (; !('0' <= (c) && (c) <= '9'); c = getchar())
    if (c == '-') f = false;
  for (; ('0' <= (c) && (c) <= '9'); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ '0');
  return f ? x : -x;
}
inline void write(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
long long n;
long long Worth[5 + 100], f[5 + 100][5 + 100][5 + 100];
char Str[5 + 100];
long long dfs(long long Left, long long Right, long long x) {
  if (Left > Right) return 0;
  if (Left == Right) return Worth[x];
  if (f[Left][Right][x]) return f[Left][Right][x];
  long long Ans = Worth[x] + dfs(Left + 1, Right, 1);
  for (register long long i = Left + 1; i <= Right; i++)
    if (Str[Left] == Str[i])
      Ans = max(Ans, dfs(Left + 1, i - 1, 1) + dfs(i, Right, x + 1));
  return f[Left][Right][x] = Ans;
}
signed main() {
  n = read();
  cin >> Str + 1;
  for (register long long i = 1; i <= n; i++) Worth[i] = read();
  write(dfs(1, n, 1));
  return 0;
}
