#include <bits/stdc++.h>
using namespace std;
int N;
int A[105];
char S[105];
long long DP[2][105][105][105], Ans[105][105], Partial[2][105];
void Read() {
  scanf("%d", &N);
  scanf("%s", S + 1);
  for (int i = 1; i <= N; i++) {
    Partial[0][i] = Partial[0][i - 1];
    Partial[1][i] = Partial[1][i - 1];
    Partial[S[i] - '0'][i]++;
  }
  for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
}
void precalcDP() {
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= N + 1; j++)
      for (int k = 0; k <= N + 1; k++)
        for (int c = 0; c <= N; c++)
          DP[i][j][k][c] = Ans[j][k] = -1000000000000;
  for (int i = 1; i <= N + 1; i++)
    for (int j = 0; j < i; j++) DP[0][i][j][0] = DP[1][i][j][0] = Ans[i][j] = 0;
  for (int dist = 1; dist <= N; dist++) {
    for (int i = 1; i + dist - 1 <= N; i++) {
      int j = i + dist - 1;
      for (int dig = 0; dig <= 1; dig++) {
        for (int cnt = 1; cnt <= Partial[dig][j] - Partial[dig][i - 1]; cnt++)
          for (int mid = i; mid <= j; mid++)
            if (S[mid] - '0' == dig) {
              DP[dig][i][j][cnt] =
                  max(DP[dig][i][j][cnt],
                      Ans[i][mid - 1] + DP[dig][mid + 1][j][cnt - 1]);
              Ans[i][j] = max(Ans[i][j], DP[dig][i][j][cnt] + A[cnt]);
            }
      }
      DP[0][i][j][0] = DP[1][i][j][0] = Ans[i][j];
    }
  }
  printf("%I64d\n", Ans[1][N]);
}
int main() {
  Read();
  precalcDP();
  return 0;
}
