#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(NULL);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  vector<long long> as(n);
  for (int i = 0; i < (int)(n); i++) cin >> as[i];
  vector<vector<long long> > ans(n + 1, vector<long long>(4, 0));
  for (int i = 0; i < (int)(n); i++) {
    if (s[i] == 'h') ans[i + 1][0] = as[i];
    ans[i + 1][0] += ans[i][0];
  }
  for (int i = 0; i < (int)(n); i++) {
    if (s[i] == 'a')
      ans[i + 1][1] = min(as[i] + ans[i][1], ans[i][0]);
    else
      ans[i + 1][1] = ans[i][1];
  }
  for (int i = 0; i < (int)(n); i++) {
    if (s[i] == 'r')
      ans[i + 1][2] = min(as[i] + ans[i][2], ans[i][1]);
    else
      ans[i + 1][2] = ans[i][2];
  }
  for (int i = 0; i < (int)(n); i++) {
    if (s[i] == 'd')
      ans[i + 1][3] = min(as[i] + ans[i][3], ans[i][2]);
    else
      ans[i + 1][3] = ans[i][3];
  }
  cout << ans[n][3] << "\n";
}
