#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
vector<vector<long long> > dp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  dp.assign(n + 1, vector<long long>(4, 1e18));
  string z = "hard";
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 4; j++) {
      if (s[i - 1] == z[j]) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i - 1]);
        if (j != 3) dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j]);
      } else {
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      }
    }
  }
  long long ans = 1e18;
  for (int i = 0; i < 4; i++) ans = min(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
