#include <bits/stdc++.h>
using namespace std;
long long dp[100005][5], v[100005];
long long n;
string str;
long long func(long long idx, long long state) {
  if (idx == n) return 0;
  if (dp[idx][state] != -1) return dp[idx][state];
  long long ans;
  if ((state == 0 && str[idx] == 'h') || (state == 1 && str[idx] == 'a') ||
      (state == 2 && str[idx] == 'r'))
    ans = min(v[idx] + func(idx + 1, state), func(idx + 1, state + 1));
  else if (state == 3 && str[idx] == 'd')
    ans = v[idx] + func(idx + 1, state);
  else
    ans = func(idx + 1, state);
  return dp[idx][state] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  cin >> str;
  for (long long i = 0; i < n; i++) cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << func(0, 0);
  return 0;
}
