#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 11;
const long long oo = 1e17;
int N;
string S;
int C[maxN];
long long A[maxN];
long long F[maxN][5];
void minimize(long long &x, long long y) { x = min(x, y); }
int main() {
  cin >> N;
  cin >> S;
  S = " " + S;
  for (int i = 1; i <= N; ++i)
    if (S[i] == 'h')
      C[i] = 1;
    else if (S[i] == 'a')
      C[i] = 2;
    else if (S[i] == 'r')
      C[i] = 3;
    else if (S[i] == 'd')
      C[i] = 4;
    else
      C[i] = 0;
  for (int i = 1; i <= N; ++i) cin >> A[i];
  for (int i = 1; i <= N; ++i)
    for (int j = 0; j <= 4; ++j) F[i][j] = oo;
  for (int i = 0; i <= N - 1; ++i)
    for (int j = 0; j <= 3; ++j) {
      if (C[i + 1] == j + 1)
        minimize(F[i + 1][j + 1], F[i][j]);
      else
        minimize(F[i + 1][j], F[i][j]);
      minimize(F[i + 1][j], F[i][j] + A[i + 1]);
    }
  cout << min(F[N][0], min(F[N][1], min(F[N][2], F[N][3])));
  return 0;
}
