#include <bits/stdc++.h>
using namespace std;
char s[105];
int A[105], cnt[105][105];
long long v[105], dp[105][105], tmp[105][105][105];
int n, w;
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  int x = 0;
  for (int i = (0); i < (n); i++) {
    if (i == 0 || s[i] == s[i - 1]) x++;
    if (i == n - 1 || s[i] != s[i + 1]) A[++w] = x, x = 1;
  }
  for (int i = (1); i <= (n); i++) scanf("%lld", &v[i]);
  for (int i = (2); i <= (n); i++) {
    for (int j = (1); j <= (i - 1); j++) v[i] = max(v[i], v[j] + v[i - j]);
  }
  for (int i = (1); i <= (w); i++)
    for (int j = i; j <= w; j += 2) {
      if (i == j)
        cnt[i][j] = A[i];
      else
        cnt[i][j] = cnt[i][j - 2] + A[j];
    }
  memset(tmp, -1, sizeof(tmp));
  for (int len = (1); len <= (w); len++) {
    for (int i = (1); i <= (w - len + 1); i++) {
      int l = i;
      int r = i + len - 1;
      if (l == r) {
        dp[l][r] = v[A[l]];
        tmp[l][l][A[l]] = 0;
        continue;
      }
      if (len % 2 == 0) {
        for (int j = (l); j <= (r); j++)
          dp[l][r] = max(dp[l][r], dp[l][j] + dp[j + 1][r]);
        continue;
      }
      for (int j = l; j < r; j += 2) {
        for (int k = (0); k <= (cnt[l][j]); k++) {
          if (tmp[l][j][k] == -1) continue;
          long long a = dp[j + 1][r - 1];
          tmp[l][r][k + A[r]] = max(tmp[l][r][k + A[r]], a + tmp[l][j][k]);
        }
      }
      for (int k = (0); k <= (cnt[l][r]); k++)
        if (tmp[l][r][k] != -1) dp[l][r] = max(dp[l][r], tmp[l][r][k] + v[k]);
    }
  }
  printf("%lld\n", dp[1][w]);
  return 0;
}
