#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
long long dp[2][5];
int a[MAXN];
char s[MAXN];
int main() {
  int n;
  long long tot = 0;
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    tot += a[i];
  }
  int now = 0, nxt = 1;
  for (int i = 1; i <= n; i++) {
    memcpy(dp[nxt], dp[now], sizeof(dp[nxt]));
    for (int j = 0; j <= 4; j++) {
      if ((s[i] == 'h' && j == 0) || (s[i] == 'a' && j == 1) ||
          (s[i] == 'r' && j == 2) || (s[i] == 'd' && j == 3))
        dp[nxt][j + 1] = max(dp[nxt][j + 1], dp[now][j] + a[i]);
      else
        dp[nxt][j] = max(dp[now][j] + a[i], dp[nxt][j]);
    }
    now ^= 1;
    nxt ^= 1;
  }
  long long ans = tot - max({dp[now][0], dp[now][1], dp[now][2], dp[now][3]});
  printf("%lld\n", ans);
  return 0;
}
