#include <bits/stdc++.h>
using namespace std;
int n, m;
bool debug = false;
int k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
char s[155];
int a[155];
int t[155][155][155], full[155][155], best[155];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  scanf("%s", s);
  memset(t, 255, sizeof t);
  for (int len = 0; len < n; len++) {
    for (int i = 0; i + len < n; i++) {
      int j = i + len;
      if (len == 0) t[i][j][1] = 0;
      full[i][j] = -1;
      for (int k = 1; k <= len + 1; k++) {
        if (s[i] == s[j]) {
          if (k > 2)
            t[i][j][k] = t[i + 1][j - 1][k - 2];
          else if (k == 2)
            t[i][j][k] = full[i + 1][j - 1];
        }
        for (int m = i + 1; m <= j; m++) {
          if (full[i][m - 1] >= 0 && t[m][j][k] >= 0)
            t[i][j][k] = max(t[i][j][k], full[i][m - 1] + t[m][j][k]);
          if (full[m][j] >= 0 && t[i][m - 1][k] >= 0)
            t[i][j][k] = max(t[i][j][k], full[m][j] + t[i][m - 1][k]);
        }
        if (a[k] >= 0 && t[i][j][k] >= 0)
          full[i][j] = max(full[i][j], t[i][j][k] + a[k]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    best[i] = max(full[0][i], i ? best[i - 1] : 0);
    for (int j = 1; j <= i; j++) {
      best[i] = max(best[i], best[j - 1] + full[j][i]);
    }
  }
  printf("%d\n", best[n - 1]);
  return 0;
}
