#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << p.first << " " << p.second;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> const &v) {
  long long int l = v.size();
  for (long long int i = 0; i < l - 1; i++) out << v[i] << ' ';
  if (l > 0) out << v[l - 1];
  return out;
}
template <typename T>
void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}
long long int n;
string second;
const long long int MAXN = 101;
long long int arr[MAXN];
long long int dp[MAXN][MAXN][MAXN];
long long int first(long long int l, long long int r, long long int k) {
  if (l >= r) return 0;
  long long int &val = dp[l][r][k];
  if (val != -1) return val;
  val = arr[k - 1] + first(l + 1, r, 1);
  for (long long int i = l + 1; i < r; i++) {
    if (second[i] == second[l]) {
      val = max(val, first(l + 1, i, 1) + first(i, r, k + 1));
    }
  }
  return val;
}
int main() {
  cin >> n;
  cin >> second;
  for (long long int i = 0; i < n; i++) cin >> arr[i];
  for (long long int i = 0; i < MAXN; i++) {
    for (long long int j = 0; j < MAXN; j++) {
      for (long long int k = 0; k < MAXN; k++) dp[i][j][k] = -1;
    }
  }
  cout << first(0, n, 1) << "\n";
  return 0;
}
