#include <bits/stdc++.h>
using namespace std;
const int _EMPTY_ = 0;
const int HAS_H = 1;
const int HAS_HA = 2;
const int HAS_HAR = 3;
int n;
char s[100010] = {0};
long long a[100010] = {0};
long long dp[10] = {0};
int main() {
  cin >> n;
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  for (int i = 0; i < 4; i++) dp[i] = -1;
  dp[_EMPTY_] = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'h') {
      if (dp[HAS_H] == -1) {
        dp[HAS_H] = dp[_EMPTY_];
      } else if (dp[_EMPTY_] < dp[HAS_H]) {
        dp[HAS_H] = dp[_EMPTY_];
      }
      dp[_EMPTY_] += a[i];
    } else if (s[i] == 'a') {
      if (dp[HAS_H] != -1) {
        if (dp[HAS_HA] == -1) {
          dp[HAS_HA] = dp[HAS_H];
        } else if (dp[HAS_H] < dp[HAS_HA]) {
          dp[HAS_HA] = dp[HAS_H];
        }
        dp[HAS_H] += a[i];
      }
    } else if (s[i] == 'r') {
      if (dp[HAS_HA] != -1) {
        if (dp[HAS_HAR] == -1) {
          dp[HAS_HAR] = dp[HAS_HA];
        } else if (dp[HAS_HA] < dp[HAS_HAR]) {
          dp[HAS_HAR] = dp[HAS_HA];
        }
        dp[HAS_HA] += a[i];
      }
    } else if (s[i] == 'd') {
      if (dp[HAS_HAR] != -1) {
        dp[HAS_HAR] += a[i];
      }
    }
  }
  for (int i = 1; i < 4; i++) {
    if (dp[i] == -1) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << min(min(dp[_EMPTY_], dp[HAS_H]), min(dp[HAS_HA], dp[HAS_HAR]))
       << endl;
  return 0;
}
