#include <bits/stdc++.h>
using namespace std;
int n, num[100005], f[100005][2];
bool g[100005][2];
string S;
vector<int> digit;
void solve(int pos, int less) {
  if (g[pos][less]) return;
  g[pos][less] = 1;
  if (pos == digit.size()) return;
  int maxd = less ? 1 : digit[pos];
  for (int d = 0; d <= maxd; d++) {
    int nless = less || d < digit[pos];
    solve(pos + 1, nless);
    if (d == 1)
      f[pos][less] = max(f[pos][less], f[pos + 1][nless] + num[pos + 1]);
    else
      f[pos][less] = max(f[pos][less], f[pos + 1][nless]);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = n; i >= 1; i--) scanf("%d", &num[i]);
  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < S.size(); i++) digit.push_back(S[i] - '0');
  solve(0, 0);
  cout << f[0][0] << endl;
  return 0;
}
