#include <bits/stdc++.h>
using namespace std;
long long point[105], answer[105][105], dp[105][105][105];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  string s;
  cin >> n >> s;
  for (int i = 1; i <= n; i++) cin >> point[i];
  for (int len = 1; len <= n; len++)
    for (int lef = 1; lef + len - 1 <= n; lef++) {
      int rig = lef + len - 1;
      bool fl = true;
      for (int i = lef; i <= rig; i++)
        if (s[i - 1] != s[lef - 1]) fl = false;
      if (fl) answer[lef][rig] = max(answer[lef][rig], point[len]);
      for (int i = lef; i < rig; i++)
        answer[lef][rig] =
            max(answer[lef][rig], answer[lef][i] + answer[i + 1][rig]);
      if (s[lef - 1] == s[rig - 1]) {
        if (lef == rig) dp[1][lef][rig] = point[1];
        if (lef != rig) {
          if (lef + 1 == rig)
            dp[2][lef][rig] = point[2];
          else
            dp[2][lef][rig] = point[2] + answer[lef + 1][rig - 1];
        }
        for (int i = 3; i <= len; i++) {
          for (int j = lef + 1; j < rig; j++)
            if (dp[i - 1][lef][j] != 0) {
              if (j + 1 == rig)
                dp[i][lef][rig] =
                    max(dp[i][lef][rig],
                        dp[i - 1][lef][j] - point[i - 1] + point[i]);
              else
                dp[i][lef][rig] =
                    max(dp[i][lef][rig], dp[i - 1][lef][j] - point[i - 1] +
                                             point[i] + answer[j + 1][rig - 1]);
            }
        }
      }
      for (int i = 1; i <= len; i++)
        answer[lef][rig] = max(answer[lef][rig], dp[i][lef][rig]);
    }
  cout << answer[1][n];
  return 0;
}
