#include <bits/stdc++.h>
using namespace std;
long long n, a[100003];
string s;
long long dp[100003][5];
long long dfs(long long st, long long state) {
  if (dp[st][state] != -1) return dp[st][state];
  if (state == 4 && st == n) return dp[st][state] = 1e18;
  if (st == n) return dp[st][state] = 0;
  long long val = 0;
  if (state == 0) {
    if (s[st] == 'h') val += a[st];
  } else if (state == 1) {
    if (s[st] == 'a') val += a[st];
  } else if (state == 2) {
    if (s[st] == 'r') val += a[st];
  } else if (state == 3) {
    if (s[st] == 'd') val += a[st];
  } else {
    return dp[st][state] = 1e18;
  }
  return dp[st][state] = min(val + dfs(st + 1, state), dfs(st + 1, state + 1));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (long long i = 0; i < 100003; i++)
    for (long long j = 0; j < 5; j++) dp[i][j] = -1;
  cin >> n >> s;
  for (long long i = 0; i < n; i++) cin >> a[i];
  cout << dfs(0, 0) << endl;
  return 0;
}
