#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long dp[10];
char s[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  cin >> s + 1;
  for (int i = 1; i <= n; i++) {
    long long a;
    cin >> a;
    if (s[i] == 'h') {
      dp[1] += a;
    }
    if (s[i] == 'a') {
      dp[2] = min(dp[2] + a, dp[1]);
    }
    if (s[i] == 'r') {
      dp[3] = min(dp[3] + a, dp[2]);
    }
    if (s[i] == 'd') {
      dp[4] = min(dp[4] + a, dp[3]);
    }
  }
  cout << dp[4] << endl;
}
