#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int maxn = 2e5 + 10;
const int maxm = 1e6 + 10;
const long long mod = 998244353;
char s[maxn];
long long x[maxn];
int main() {
  int n;
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) scanf("%lld", &x[i]);
  long long a, b, c, d;
  a = b = c = d = 0;
  if (s[1] == 'h') a = x[1];
  for (int i = 2; i <= n; i++) {
    if (s[i] == 'h')
      a += x[i];
    else if (s[i] == 'a')
      b = min(a, b + x[i]);
    else if (s[i] == 'r')
      c = min(b, c + x[i]);
    else if (s[i] == 'd')
      d = min(c, d + x[i]);
  }
  printf("%lld\n", d);
  return 0;
}
