#include <bits/stdc++.h>
using namespace std;
const long long INF = 1L << 60;
const int maxn = 1e5 + 3;
long long dp[maxn][4];
int a[maxn];
int n;
string s;
string t = "hard";
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  for (int i = 0; i <= n - 1; i++)
    for (int j = 0; j <= 3; j++) dp[i][j] = INF;
  for (int i = 0; i <= n - 1; i++) cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= 3; j++) {
      if (s[i] != t[j])
        dp[i][j] = (i == n - 1 ? 0 : dp[i + 1][j]);
      else if (j < 3)
        dp[i][j] = min(dp[i + 1][j + 1], a[i] + dp[i + 1][j]);
      else
        dp[i][j] = a[i] + dp[i + 1][j];
    }
  }
  cout << dp[0][0] << '\n';
  return 0;
}
