#include <bits/stdc++.h>
using namespace std;
void chmax(int64_t& a, int64_t b) { a = max(a, b); }
int main() {
  int N;
  string S;
  cin >> N >> S;
  int64_t A[101] = {0};
  for (int i = 1; i <= N; i++) cin >> A[i];
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j < i; j++) A[i] = max(A[i], A[j] + A[i - j]);
  }
  vector<pair<int, int>> B;
  int len = 1;
  for (int i = 1; i <= N; i++) {
    if (i < N && S[i - 1] == S[i]) {
      len++;
    } else {
      B.push_back({S[i - 1] - '0', len});
      len = 1;
    }
  }
  int M = B.size();
  int sum[101] = {0};
  for (int i = 0; i < M; i++) {
    sum[i] = B[i].second;
    if (i > 0) sum[i] += sum[i - 1];
  }
  const int64_t INF = 1e18;
  static int64_t dp[100][100][101], mx[100][100];
  for (int i = 0; i < M; i++)
    for (int j = 0; j < M; j++) {
      for (int k = 0; k <= N; k++) dp[i][j][k] = -INF;
      mx[i][j] = -INF;
    }
  for (int i = 0; i < M; i++) dp[i][i][B[i].second] = mx[i][i] = A[B[i].second];
  for (int d = 2; d <= M; d++)
    for (int i = 0; i + d <= M; i++) {
      int j = i + d - 1;
      dp[i][j][B[i].second] = A[B[i].second] + mx[i + 1][j];
      for (int k = i + 2; k <= j; k += 2) {
        int s = sum[j] - sum[k - 1];
        for (int l = 0; l <= s; l++) {
          int l2 = B[i].second + l;
          chmax(dp[i][j][l2], mx[i + 1][k - 1] + dp[k][j][l] - A[l] + A[l2]);
        }
      }
      for (int l = 0; l <= N; l++) chmax(mx[i][j], dp[i][j][l]);
    }
  cout << mx[0][M - 1] << endl;
  return 0;
}
