#include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
int main() {
int n; cin >> n;
string s; cin >> s;
long long a[n];
for(int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int> > pos(4);
string t = "hard";
long long amb[4]; memset(amb, 0, sizeof amb);
for(int j = 0; j < 4; j++) {
for(int i = 0; i < n; i++) {
if(s[i] == t[j]) {
pos[j].push_back(i);
}
}
if(SZ(pos[j]) == 0) {
cout << 0 << endl; return 0;
}
}
for(int i = 0; i < SZ(pos[0]); i++) {
if(pos[0][i] < pos[1][SZ(pos[1]) - 1]) {
amb[0] += a[pos[0][i]];
}
}
for(int i = 1; i < 3; i++) {
for(int j = 0; j < pos[i].size(); j++) {
if(pos[i][j] < pos[i + 1][SZ(pos[i + 1]) - 1] && pos[i][j] > pos[i - 1][pos[i - 1][0]]) {
amb[i] += a[pos[i][j]];
}
}
}
for(int i = 0; i < SZ(pos[3]); i++) {
if(pos[3][i] > pos[2][0]) {
amb[3] += a[pos[3][i]];
}
}
cout << min(amb[0], min(amb[1], min(amb[2], amb[3])));
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIFNaKHgpICgoaW50KSh4KS5zaXplKCkpCgppbnQgbWFpbigpIHsKICBpbnQgbjsgY2luID4+IG47CiAgc3RyaW5nIHM7IGNpbiA+PiBzOwogIGxvbmcgbG9uZyBhW25dOwogIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgIGNpbiA+PiBhW2ldOwogIH0KICB2ZWN0b3I8dmVjdG9yPGludD4gPiBwb3MoNCk7CiAgc3RyaW5nIHQgPSAiaGFyZCI7CiAgbG9uZyBsb25nIGFtYls0XTsgbWVtc2V0KGFtYiwgMCwgc2l6ZW9mIGFtYik7CiAgZm9yKGludCBqID0gMDsgaiA8IDQ7IGorKykgewogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICBpZihzW2ldID09IHRbal0pIHsKICAgICAgICBwb3Nbal0ucHVzaF9iYWNrKGkpOwogICAgICB9CiAgICB9CiAgICBpZihTWihwb3Nbal0pID09IDApIHsKICAgICAgY291dCA8PCAwIDw8IGVuZGw7IHJldHVybiAwOwogICAgfQogIH0KICBmb3IoaW50IGkgPSAwOyBpIDwgU1oocG9zWzBdKTsgaSsrKSB7CiAgICBpZihwb3NbMF1baV0gPCBwb3NbMV1bU1oocG9zWzFdKSAtIDFdKSB7CiAgICAgIGFtYlswXSArPSBhW3Bvc1swXVtpXV07CiAgICB9CiAgfQogIGZvcihpbnQgaSA9IDE7IGkgPCAzOyBpKyspIHsKICAgIGZvcihpbnQgaiA9IDA7IGogPCBwb3NbaV0uc2l6ZSgpOyBqKyspIHsKICAgICAgaWYocG9zW2ldW2pdIDwgcG9zW2kgKyAxXVtTWihwb3NbaSArIDFdKSAtIDFdICYmIHBvc1tpXVtqXSA+IHBvc1tpIC0gMV1bcG9zW2kgLSAxXVswXV0pIHsKICAgICAgICBhbWJbaV0gKz0gYVtwb3NbaV1bal1dOwogICAgICB9CiAgICB9CiAgfQogIGZvcihpbnQgaSA9IDA7IGkgPCBTWihwb3NbM10pOyBpKyspIHsKICAgIGlmKHBvc1szXVtpXSA+IHBvc1syXVswXSkgewogICAgICBhbWJbM10gKz0gYVtwb3NbM11baV1dOwogICAgfQogIH0KICBjb3V0IDw8IG1pbihhbWJbMF0sIG1pbihhbWJbMV0sIG1pbihhbWJbMl0sIGFtYlszXSkpKTsKICByZXR1cm4gMDsKfQ==