#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100100, md = 998244353;
long long a[MAXN];
long long dp[26][MAXN];
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long n;
  string s;
  cin >> n;
  cin >> s;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 0; i < 26; i++)
    for (long long j = 0; j < MAXN; j++) dp[i][j] = 9000000000000000000ll;
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[2][0] = 0;
  dp[3][0] = 0;
  s = "#" + s;
  for (long long i = 1; i <= n; i++) {
    if (s[i] == 'h') {
      dp[0][i] = dp[0][i - 1] + a[i];
      dp[1][i] = min(dp[1][i - 1], dp[0][i - 1]);
      dp[2][i] = dp[2][i - 1];
      dp[3][i] = dp[3][i - 1];
    } else if (s[i] == 'a') {
      dp[0][i] = dp[0][i - 1];
      dp[1][i] = dp[1][i - 1] + a[i];
      dp[2][i] = min(dp[2][i - 1], dp[1][i - 1]);
      dp[3][i] = dp[3][i - 1];
    } else if (s[i] == 'r') {
      dp[0][i] = dp[0][i - 1];
      dp[1][i] = dp[1][i - 1];
      dp[2][i] = dp[2][i - 1] + a[i];
      dp[3][i] = min(dp[3][i - 1], dp[2][i - 1]);
    } else if (s[i] == 'd') {
      dp[0][i] = dp[0][i - 1];
      dp[1][i] = dp[1][i - 1];
      dp[2][i] = dp[2][i - 1];
      dp[3][i] = dp[3][i - 1] + a[i];
    } else {
      dp[0][i] = dp[0][i - 1];
      dp[1][i] = dp[1][i - 1];
      dp[2][i] = dp[2][i - 1];
      dp[3][i] = dp[3][i - 1];
    }
  }
  cout << min(min(dp[0][n], dp[1][n]), min(dp[2][n], dp[3][n])) << '\n';
}
