#include <bits/stdc++.h>
using namespace std;
const long long NR = 5e5 + 100;
const long long oo = 2e9 + 100;
const long long MOD = 998244353;
long long n;
string s;
long long a[NR];
long long dp[5];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long i;
  cin >> n;
  cin >> s;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (i = 0; i < s.size(); ++i) {
    if (s[i] == 'h') {
      dp[0] += a[i];
    }
    if (s[i] == 'a') {
      dp[1] = min(dp[1] + a[i], dp[0]);
    }
    if (s[i] == 'r') {
      dp[2] = min(a[i] + dp[2], dp[1]);
    }
    if (s[i] == 'd') {
      dp[3] = min(a[i] + dp[3], dp[2]);
    }
  }
  cout << dp[3] << '\n';
  return 0;
}
