#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  long long amb[n];
  for (long long i = 0; i < n; ++i) cin >> amb[i];
  long long adp[4] = {}, cdp[4] = {};
  if (s[n - 1] == 'd') {
    adp[3] = amb[n - 1];
    cdp[3] = 1;
  }
  for (long long i = n - 2; i >= 0; --i) {
    if (s[i] == 'd') {
      adp[3] += amb[i];
    }
    if (s[i] == 'r') {
      adp[2] = min(amb[i] + adp[2], adp[3]);
    }
    if (s[i] == 'a') {
      adp[1] = min(amb[i] + adp[1], adp[2]);
    }
    if (s[i] == 'h') {
      adp[0] = min(amb[i] + adp[0], adp[1]);
    }
  }
  cout << adp[0];
}
