#include #define endl "\n" using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long long INF = 1e18; //const ll mod = 1000000007; ll N; string S; vector V; ll ans = 0; vector s; string yuki = "yuki"; bool used[2005]; l_l dp[2005][5]; void f(ll s) { for(int i = s; i <= N; i++) { for(int j = 0; j < 5; j++) { dp[i][j] = {-INF, 0}; } } dp[s][0] = {0, -1}; for(int i = s; i < N; i++) { for(int j = 0; j < 5; j++) { chmax(dp[i+1][j], dp[i][j]); if(j < 4 and !used[i] and S[i] == yuki[j]) { l_l now = {dp[i][j].first + V[i], i}; chmax(dp[i+1][j+1], now); } } } /* for(int j = 0; j < 5; j++) { for(int i = s; i <= N; i++) { cerr << "{" << dp[i][j].first << ", " << dp[i][j].second << "} "; } cerr << endl; } */ ll nowi = N; ll nowj = 4; ll nowans = 0; while(nowj != 0) { ll newi = dp[nowi][nowj].second; nowans += V[newi]; used[newi] = true; nowi = newi; nowj--; } ans += nowans; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> S; V.resize(N); for(int i = 0; i < N; i++) cin >> V[i]; for(int i = 0; i < N; i++) { if(S[i] != 'y') continue; vector v; for(int j = i; j < N; j++) { if(v.size() == yuki.size()) continue; if(used[j]) continue; if(S[j] == yuki[v.size()]) v.push_back(j); } if(v.size() != yuki.size()) continue; s.push_back(i); for(auto j : v) used[j] = true; } /* for(auto tmp : s) cerr << tmp << " "; cerr << endl; */ for(int i = 0; i < N; i++) used[i] = false; reverse(s.begin(), s.end()); for(auto tmp : s) { f(tmp); } cout << ans << endl; return 0; }