#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100005;
string s;
int cost[NMAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  string a = "$";
  cin >> s;
  s = a + s;
  for (long long i = 1; i <= n; i++) cin >> cost[i];
  long long costh = 0, costa = 0, costr = 0, costd = 0;
  string temp = "";
  long long sum = 0;
  bool ok = false;
  for (long long i = 1; i <= n; i++) {
    if (s[i] == 'h')
      costh += cost[i];
    else if (s[i] == 'a')
      costa = min(costh, costa + cost[i]);
    else if (s[i] == 'r')
      costr = min(costa, costr + cost[i]);
    else if (s[i] == 'd')
      costd = min(costr, costd + cost[i]);
  }
  cout << costd;
  return 0;
}
