#include <bits/stdc++.h>
using namespace std;
long long h[100005];
long long dp[100005][5];
char str[100005];
int main() {
  int n;
  long long sum = 0;
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &h[i]);
    sum += h[i];
  }
  for (int i = 1; i <= n; i++) {
    if (str[i] == 'h') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + h[i];
      dp[i][2] = dp[i - 1][2] + h[i];
      dp[i][3] = dp[i - 1][3] + h[i];
      dp[i][4] = dp[i - 1][4] + h[i];
    } else if (str[i] == 'a') {
      dp[i][0] = dp[i - 1][0] + h[i];
      dp[i][1] = dp[i - 1][1];
      dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + h[i];
      dp[i][3] = dp[i - 1][3] + h[i];
      dp[i][4] = dp[i - 1][4] + h[i];
    } else if (str[i] == 'r') {
      dp[i][0] = dp[i - 1][0] + h[i];
      dp[i][1] = dp[i - 1][1] + h[i];
      dp[i][2] = dp[i - 1][2];
      dp[i][3] = max(dp[i - 1][2], dp[i - 1][3]) + h[i];
      dp[i][4] = dp[i - 1][4] + h[i];
    } else if (str[i] == 'd') {
      dp[i][0] = dp[i - 1][0] + h[i];
      dp[i][1] = dp[i - 1][1] + h[i];
      dp[i][2] = dp[i - 1][2] + h[i];
      dp[i][3] = dp[i - 1][3];
      dp[i][4] = max(dp[i - 1][3], dp[i - 1][4]) + h[i];
    } else {
      dp[i][0] = dp[i - 1][0] + h[i];
      dp[i][1] = dp[i - 1][1] + h[i];
      dp[i][2] = dp[i - 1][2] + h[i];
      dp[i][3] = dp[i - 1][3] + h[i];
      dp[i][4] = dp[i - 1][4] + h[i];
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[n][i]);
  }
  printf("%I64d\n", sum - ans);
  return 0;
}
