#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
long long n, a[N], dp[N][8];
string s;
long long fun(long long i, long long mask) {
  if (i == n) return 0;
  if (dp[i][mask] != -1) return dp[i][mask];
  long long minn, r;
  minn = LLONG_MAX;
  if (s[i] == 'h' && !(mask & 1 << 0)) {
    r = a[i] + fun(i + 1, mask);
    minn = min(minn, r);
    r = fun(i + 1, mask | 1 << 0);
    minn = min(minn, r);
  } else if (s[i] == 'a' && (mask & 1 << 0)) {
    r = a[i] + fun(i + 1, mask);
    minn = min(minn, r);
    r = fun(i + 1, mask | 1 << 1);
    minn = min(minn, r);
  } else if (s[i] == 'r' && (mask & 1 << 1)) {
    r = a[i] + fun(i + 1, mask);
    minn = min(minn, r);
    r = fun(i + 1, mask | 1 << 2);
    minn = min(minn, r);
  } else if (s[i] == 'd' && (mask & 1 << 2))
    minn = a[i] + fun(i + 1, mask);
  else
    minn = fun(i + 1, mask);
  dp[i][mask] = minn;
  return minn;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long i, j, k, r, t;
  cin >> n;
  cin >> s;
  for (i = 0; i < n; i++) cin >> a[i];
  long long ans;
  memset(dp, -1, sizeof(dp));
  ans = fun(0, 0);
  cout << ans << "\n";
  return 0;
}
