#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 100025;
long long sto[4][maxn], tmp[maxn];
char str[maxn];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; i++) scanf("%lld", &tmp[i]);
  for (int i = 1; i <= n; i++) {
    sto[0][i] = sto[0][i - 1] + tmp[i] * (str[i] == 'h');
    sto[1][i] = min(sto[0][i], sto[1][i - 1] + tmp[i] * (str[i] == 'a'));
    sto[2][i] = min(sto[1][i], sto[2][i - 1] + tmp[i] * (str[i] == 'r'));
    sto[3][i] = min(sto[2][i], sto[3][i - 1] + tmp[i] * (str[i] == 'd'));
  }
  printf("%lld\n", min(min(sto[0][n], sto[1][n]), min(sto[2][n], sto[3][n])));
  return 0;
}
