#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6;
const int inf = 1e9;
const int mod = 1e9 + 7;
long long dp[MAXN][4];
long long w[MAXN];
int32_t main() {
  long long n, m = 4;
  cin >> n;
  string s, t = "hard";
  cin >> s;
  for (long long i = 0; i < n; i++) {
    cin >> w[i];
  }
  for (long long i = n - 1; i >= 0; i--) {
    for (long long j = m - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        if (j == m - 1)
          dp[i][j] = dp[i + 1][j] + w[i];
        else
          dp[i][j] = min(dp[i + 1][j + 1], dp[i + 1][j] + w[i]);
      } else {
        dp[i][j] = dp[i + 1][j];
      }
    }
  }
  cout << dp[0][0] << endl;
}
