#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
inline void debug_vi(vector<int> a) {
  for (long long i = (long long)(0); i < (long long)(a.size()); i++)
    cout << a[i] << " ";
}
inline void debug_vll(vector<long long> a) {
  for (long long i = (long long)(0); i < (long long)(a.size()); i++)
    cout << a[i] << " ";
}
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
const int nax = 105;
int n;
long long dp[nax][nax][nax];
string s;
int best[nax];
long long solve(int l, int r, int left) {
  if (l > r) return 0;
  if (l == r) return best[left + 1];
  long long &res = dp[l][r][left];
  if (res >= 0) return res;
  res = max(res, best[left + 1] + solve(l + 1, r, 0));
  for (long long i = (long long)(l + 1); i < (long long)(r + 1); i++) {
    if (s[l] == s[i]) {
      res = max(res, solve(l + 1, i - 1, 0) + solve(i, r, left + 1));
    }
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> s;
  for (long long i = (long long)(1); i < (long long)(n + 1); i++)
    cin >> best[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(0, n - 1, 0);
  return 0;
}
