#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
#define int long long
const int maxn = 1.1e5, INF = 1e18;
int dp[maxn][200], n;
string s; vector<int> v;
string hard = "hard";
// 7
// hardard
// 10 10 1 10 10 10 1
int dpp(int i, int j){
if(i == n) return dp[i][j] = 0;
if(dp[i][j] != -1) return dp[i][j];
// s[i] eh a letra q eu to agora
// j eh a letra q eu to procurando
int tirar = v[i]+dpp(i+1, j);
int next = j;
if(s[i] == j){
if(s[i] == 'd'){
return dp[i][j] = tirar;
}
for(int k = 0; k < 3; k++){
if(hard[k] == s[i]){
next = hard[k+1];
}
}
}
int ficar = dpp(i+1, next);
return dp[i][j] = min(tirar, ficar);
}
void solve() {
cin >> n >> s;
for(int i = 0; i <= n; i++){
for(int j = 0; j < 200; j++) dp[i][j] = -1;
}
for(int i = 0; i < n; i++){
int x; cin >> x;
v.push_back(x);
}
dpp(0, 'h');
// int res = INF;
// for(int i = 0; i < n; i++){
// for(char c : hard){
// if(dp[i][c] != -1) res = min(res, dp[i][c]);
// }
// }
cout << dp[0]['h'] << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
260C - Balls and Boxes | 1554A - Cherry |
11B - Jumping Jack | 716A - Crazy Computer |
644A - Parliament of Berland | 1657C - Bracket Sequence Deletion |
1657B - XY Sequence | 1009A - Game Shopping |
1657A - Integer Moves | 230B - T-primes |
630A - Again Twenty Five | 1234D - Distinct Characters Queries |
1183A - Nearest Interesting Number | 1009E - Intercity Travelling |
1637B - MEX and Array | 224A - Parallelepiped |
964A - Splits | 1615A - Closing The Gap |
4C - Registration System | 1321A - Contest for Robots |
1451A - Subtract or Divide | 1B - Spreadsheet |
1177A - Digits Sequence (Easy Edition) | 1579A - Casimir's String Solitaire |
287B - Pipeline | 510A - Fox And Snake |
1520B - Ordinary Numbers | 1624A - Plus One on the Subset |
350A - TL | 1487A - Arena |