#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline int chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline int chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
const int MAXM = 5;
int n;
string s;
int a[MAXN];
void read() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) cin >> a[i];
}
int64_t dp[MAXN][MAXM];
string T = "hard";
int64_t rec(int i, int j) {
  if (j == ((int)T.size())) return (int64_t)1e18;
  if (i == n) return 0;
  int64_t &memo = dp[i][j];
  if (memo != -1) return memo;
  memo = a[i] + rec(i + 1, j);
  if (s[i] == T[j])
    chkmin(memo, rec(i + 1, j + 1));
  else
    chkmin(memo, rec(i + 1, j));
  return memo;
}
void solve() {
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
