#include <bits/stdc++.h>
using namespace std;
string s;
long long a[100010];
long long dp[5];
int main() {
  int n;
  scanf("%d", &n);
  cin >> s;
  s = '0' + s;
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'h') {
      dp[1] = dp[1] + a[i];
    } else if (s[i] == 'a') {
      dp[2] = min(dp[1], dp[2] + a[i]);
    } else if (s[i] == 'r') {
      dp[3] = min(dp[2], dp[3] + a[i]);
    } else if (s[i] == 'd') {
      dp[4] = min(dp[3], dp[4] + a[i]);
    }
  }
  printf("%lld", dp[4]);
}
