#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
ll dp[100005][4];
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0), cout.precision(15);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, 0x3f, sizeof(dp));
  if (s[0] == 'h') {
    dp[0][0] = a[0];
    dp[0][1] = 0;
  } else {
    dp[0][0] = 0;
  }
  for (int i = 1; i < n; i++) {
    if (s[i] == 'h') {
      dp[i][0] = a[i] + dp[i - 1][0];
    } else {
      dp[i][0] = dp[i - 1][0];
    }
    if (s[i] == 'h') {
      dp[i][1] = min(dp[i][1], dp[i - 1][0]);
      dp[i][1] = min(dp[i][1], dp[i - 1][1]);
    } else if (s[i] == 'a') {
      dp[i][1] = min(dp[i][1], dp[i - 1][1] + a[i]);
    } else {
      dp[i][1] = min(dp[i][1], dp[i - 1][1]);
    }
    if (s[i] == 'a') {
      dp[i][2] = min(dp[i][2], dp[i - 1][1]);
      dp[i][2] = min(dp[i][2], dp[i - 1][2]);
    } else if (s[i] == 'r') {
      dp[i][2] = min(dp[i][2], dp[i - 1][2] + a[i]);
    } else {
      dp[i][2] = min(dp[i][2], dp[i - 1][2]);
    }
    if (s[i] == 'r') {
      dp[i][3] = min(dp[i][3], dp[i - 1][2]);
      dp[i][3] = min(dp[i][3], dp[i - 1][3]);
    } else if (s[i] == 'd') {
      dp[i][3] = min(dp[i][3], dp[i - 1][3] + a[i]);
    } else {
      dp[i][3] = min(dp[i][3], dp[i - 1][3]);
    }
  }
  ll res = 0x3f3f3f3f3f3f3f3fLL;
  for (int i = 0; i < 4; i++) {
    res = min(res, dp[n - 1][i]);
  }
  cout << res << endl;
  return 0;
}
