#include <bits/stdc++.h>
using namespace std;
long long const MX = 1e5 + 5;
long long const INF = 1e18;
long long n;
string s;
long long dp[MX][5];
bool val[MX][5];
long long funcion(long long pos, long long cont, vector<long long> &v) {
  if (cont >= 4) return INF;
  if (pos > n) {
    if (cont > 4)
      return INF;
    else
      return dp[pos - 1][cont];
  }
  if (val[pos][cont]) return dp[pos][cont];
  val[pos][cont] = true;
  long long a, b;
  if ((s[pos] == 'h' && cont == 0) || (s[pos] == 'a' && cont == 1) ||
      (s[pos] == 'r' && cont == 2) || (s[pos] == 'd' && cont == 3)) {
    a = funcion(pos + 1, cont, v) + v[pos];
    b = funcion(pos + 1, cont + 1, v);
    dp[pos][cont] = min(a, b);
  } else {
    dp[pos][cont] = funcion(pos + 1, cont, v);
  }
  return dp[pos][cont];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vector<long long> v(n);
  cin >> s;
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << funcion(0ll, 0ll, v);
  return 0;
}
