#include <bits/stdc++.h>
using namespace std;
char s[105];
long long dp[100 + 5][100 + 5][100 + 5];
long long a[100 + 5];
long long n;
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cin >> s + 1;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i - 1; j++) {
      a[i] = max(a[i], a[i - j] + a[j]);
    }
  for (int len = 1; len <= n; len++)
    for (int i = 1; i + len - 1 <= n; i++) {
      long long j = i + len - 1;
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = max(dp[i][j][k], dp[i + 1][j][0] + a[k + 1]);
        for (int p = i + 1; p <= j; p++) {
          if (s[i] == s[p]) {
            dp[i][j][k] =
                max(dp[i][j][k], dp[i + 1][p - 1][0] + dp[p][j][k + 1]);
          }
        }
      }
    }
  cout << dp[1][n][0];
}
