#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
long long c[105], dp[101][101][101];
int32_t main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  long long n;
  string s;
  cin >> n >> s;
  for (long long i = 1; i <= n; i++) cin >> c[i];
  for (long long len = 1; len <= n; len++) {
    for (long long i = 0; i + len - 1 < n; i++) {
      long long j = i + len - 1;
      for (long long k = 1; k <= n; k++) {
        dp[i][j][k] = dp[i + 1][j][1] + c[k];
        for (long long l = i + 1; l <= j; l++) {
          if (s[l] == s[i])
            dp[i][j][k] =
                max(dp[i][j][k], dp[i + 1][l - 1][1] + dp[l][j][k + 1]);
        }
      }
    }
  }
  cout << dp[0][n - 1][1] << endl;
}
