#include <bits/stdc++.h>
using namespace std;
long long ambiguity[112345];
long long fin_ans[4];
void solve() {
  int n;
  cin >> n;
  string st;
  cin >> st;
  for (int i = 0; i < 4; i++) fin_ans[i] = 0;
  fin_ans[0] = 0;
  for (int i = 0; i < n; i++) scanf("%lld", &ambiguity[i]);
  for (int i = 0; i < n; i++) {
    if (st[i] == 'h') {
      if (fin_ans[1] == -1) fin_ans[1] = min(fin_ans[0], fin_ans[1]);
      fin_ans[0] = fin_ans[0] + ambiguity[i];
    }
    if (st[i] == 'a') {
      if (fin_ans[2] == -1) fin_ans[2] = min(fin_ans[2], fin_ans[1]);
      fin_ans[1] = fin_ans[1] + ambiguity[i];
    }
    if (st[i] == 'r') {
      if (fin_ans[3] == -1) fin_ans[3] = min(fin_ans[3], fin_ans[2]);
      fin_ans[2] = fin_ans[2] + ambiguity[i];
    }
    if (st[i] == 'd') {
      fin_ans[3] = fin_ans[3] + ambiguity[i];
    }
    long long curr_min = fin_ans[0];
    for (int j = 0; j <= 3; j++) {
      curr_min = min(curr_min, fin_ans[j]);
      fin_ans[j] = curr_min;
    }
  }
  cout << min(min(min(fin_ans[0], fin_ans[1]), fin_ans[2]), fin_ans[3]) << endl;
}
int main() {
  int test = 1;
  int tmp;
  while (test--) {
    solve();
  }
}
