#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
long long f[2][5];
int a[MAXN];
char s[MAXN];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int now = 1;
  for (int i = 1; i <= n; i++) {
    int last = now ^ 1;
    if (s[i] == 'h') {
      for (int j = 0; j <= 3; j++) f[now][j] = f[last][j];
      f[now][1] = min(f[now][1], f[now][0]);
      f[now][0] = f[last][0] + a[i];
    } else if (s[i] == 'a') {
      for (int j = 0; j <= 3; j++) f[now][j] = f[last][j];
      f[now][2] = min(f[now][2], f[now][1]);
      f[now][1] = f[last][1] + a[i];
    } else if (s[i] == 'r') {
      for (int j = 0; j <= 3; j++) f[now][j] = f[last][j];
      f[now][3] = min(f[now][3], f[now][2]);
      f[now][2] = f[last][2] + a[i];
    } else if (s[i] == 'd') {
      for (int j = 0; j <= 3; j++) f[now][j] = f[last][j];
      f[now][3] = f[last][3] + a[i];
    } else {
      for (int j = 0; j <= 3; j++) f[now][j] = f[last][j];
    }
    now ^= 1;
  }
  long long ans = 1e18;
  for (int i = 0; i <= 3; i++) ans = min(ans, f[now ^ 1][i]);
  cout << ans << endl;
  return 0;
}
