#include <bits/stdc++.h>
#define int long long
#define faster() ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;
const int N = 1e5 + 1;
const int INF_LL = 1e18;
int dp[N][4];
void solve() {
int n;
cin >> n;
string s;
cin >> s;
s = " " + s; // Thêm một khoảng trắng vào đầu chuỗi để dễ quản lý chỉ số
// Khởi tạo dp
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= 3; ++j) {
dp[i][j] = INF_LL; // Khởi tạo tất cả giá trị bằng INF_LL
}
}
dp[0][0] = 0 ;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
if (s[i] == 'h') {
dp[i][0] = dp[i - 1][0] + x;
dp[i][1] = min(dp[i - 1][1], dp[i - 1][0]);
dp[i][2] = dp[i - 1][2];
dp[i][3] = dp[i - 1][3];
}
else if (s[i] == 'a') {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1] + x;
dp[i][2] = min(dp[i - 1][2], dp[i - 1][1]);
dp[i][3] = dp[i - 1][3];
}
else if (s[i] == 'r') {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1];
dp[i][2] = dp[i - 1][2] + x;
dp[i][3] = min(dp[i - 1][3], dp[i - 1][2]);
}
else if (s[i] == 'd') {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1];
dp[i][2] = dp[i - 1][2];
dp[i][3] = dp[i - 1][3] + x;
}
else {
for (int j = 0; j <= 3; ++j) {
dp[i][j] = dp[i - 1][j];
}
}
}
cout << min({dp[n][0], dp[n][1], dp[n][2], dp[n][3]}) << "\n";
}
signed main() {
faster();
solve();
return 0;
}
CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBmYXN0ZXIoKSBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUobnVsbHB0cik7IGNvdXQudGllKG51bGxwdHIpOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSAxZTUgKyAxOwpjb25zdCBpbnQgSU5GX0xMID0gMWUxODsKaW50IGRwW05dWzRdOwoKdm9pZCBzb2x2ZSgpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICBzdHJpbmcgczsKICAgIGNpbiA+PiBzOwogICAgcyA9ICIgIiArIHM7IC8vIFRow6ptIG3hu5l0IGtob+G6o25nIHRy4bqvbmcgdsOgbyDEkeG6p3UgY2h14buXaSDEkeG7gyBk4buFIHF14bqjbiBsw70gY2jhu4kgc+G7kQogICAgLy8gS2jhu59pIHThuqFvIGRwCiAgICBmb3IoaW50IGkgPSAwOyBpIDw9IG47ICsraSkgewogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPD0gMzsgKytqKSB7CiAgICAgICAgICAgIGRwW2ldW2pdID0gSU5GX0xMOyAvLyBLaOG7n2kgdOG6oW8gdOG6pXQgY+G6oyBnacOhIHRy4buLIGLhurFuZyBJTkZfTEwKICAgICAgICB9CiAgICB9CgogICAgZHBbMF1bMF0gPSAwIDsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICBpbnQgeDsKICAgICAgICBjaW4gPj4geDsKICAgICAgICBpZiAoc1tpXSA9PSAnaCcpIHsKICAgICAgICAgICAgZHBbaV1bMF0gPSBkcFtpIC0gMV1bMF0gKyB4OwogICAgICAgICAgICBkcFtpXVsxXSA9IG1pbihkcFtpIC0gMV1bMV0sIGRwW2kgLSAxXVswXSk7CiAgICAgICAgICAgIGRwW2ldWzJdID0gZHBbaSAtIDFdWzJdOwogICAgICAgICAgICBkcFtpXVszXSA9IGRwW2kgLSAxXVszXTsKICAgICAgICB9IAogICAgICAgIGVsc2UgaWYgKHNbaV0gPT0gJ2EnKSB7CiAgICAgICAgICAgIGRwW2ldWzBdID0gZHBbaSAtIDFdWzBdOwogICAgICAgICAgICBkcFtpXVsxXSA9IGRwW2kgLSAxXVsxXSArIHg7CiAgICAgICAgICAgIGRwW2ldWzJdID0gbWluKGRwW2kgLSAxXVsyXSwgZHBbaSAtIDFdWzFdKTsKICAgICAgICAgICAgZHBbaV1bM10gPSBkcFtpIC0gMV1bM107CiAgICAgICAgfSAKICAgICAgICBlbHNlIGlmIChzW2ldID09ICdyJykgewogICAgICAgICAgICBkcFtpXVswXSA9IGRwW2kgLSAxXVswXTsKICAgICAgICAgICAgZHBbaV1bMV0gPSBkcFtpIC0gMV1bMV07CiAgICAgICAgICAgIGRwW2ldWzJdID0gZHBbaSAtIDFdWzJdICsgeDsKICAgICAgICAgICAgZHBbaV1bM10gPSBtaW4oZHBbaSAtIDFdWzNdLCBkcFtpIC0gMV1bMl0pOwogICAgICAgIH0gCiAgICAgICAgZWxzZSBpZiAoc1tpXSA9PSAnZCcpIHsKICAgICAgICAgICAgZHBbaV1bMF0gPSBkcFtpIC0gMV1bMF07CiAgICAgICAgICAgIGRwW2ldWzFdID0gZHBbaSAtIDFdWzFdOwogICAgICAgICAgICBkcFtpXVsyXSA9IGRwW2kgLSAxXVsyXTsKICAgICAgICAgICAgZHBbaV1bM10gPSBkcFtpIC0gMV1bM10gKyB4OwogICAgICAgIH0KICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDw9IDM7ICsraikgewogICAgICAgICAgICAgICAgZHBbaV1bal0gPSBkcFtpIC0gMV1bal07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IG1pbih7ZHBbbl1bMF0sIGRwW25dWzFdLCBkcFtuXVsyXSwgZHBbbl1bM119KSA8PCAiXG4iOwp9CgpzaWduZWQgbWFpbigpIHsKICAgIGZhc3RlcigpOwogICAgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cg==