#include <bits/stdc++.h>
using namespace std;
int a[100010], n;
long long dp[100010][6];
string t = "hard", s;
long long f(int i, int len) {
  if (len == 4) return (long long)1e15;
  if (i == n) return 0;
  long long &ret = dp[i][len];
  if (ret != -1) return ret;
  ret = min((long long)1e15, f(i + 1, len) + a[i]);
  ret = min(ret, f(i + 1, len + (s[i] == t[len])));
  ret = min(ret, (long long)1e15);
  return ret;
}
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << f(0, 0) << endl;
  return 0;
}
