#include <bits/stdc++.h>
const int N = 500500;
const int Q = 1 << 21;
const int mod = 1e9 + 7;
using namespace std;
int main() {
  long long n;
  cin >> n;
  char ch[n + 1];
  for (long long i = 1; i <= n; i++) cin >> ch[i];
  long long arr[n + 1];
  for (long long i = 1; i <= n; i++) cin >> arr[i];
  long long ans[4][n + 1];
  ans[0][0] = ans[1][0] = ans[2][0] = ans[3][0] = 0;
  for (long long i = 1; i <= n; i++) {
    ans[0][i] = ans[0][i - 1] + arr[i] * (ch[i] == 'h');
  }
  for (long long i = 1; i <= n; i++) {
    ans[1][i] = min(ans[0][i], ans[1][i - 1] + arr[i] * (ch[i] == 'a'));
  }
  for (long long i = 1; i <= n; i++) {
    ans[2][i] = min(ans[1][i], ans[2][i - 1] + arr[i] * (ch[i] == 'r'));
  }
  for (long long i = 1; i <= n; i++) {
    ans[3][i] = min(ans[2][i], ans[3][i - 1] + arr[i] * (ch[i] == 'd'));
  }
  cout << ans[3][n];
}
