#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #include using namespace atcoder; #define REP(i,n) for(int i = 0; i < (int)n; i++) #define RREP(i,n) for(int i = (int)n-1; i >= 0; i--) #define LREP(i,n) for(LL i = 0; i < (LL)n; i++) #define Vi vector #define Vl vector #define LP pair #define P pair #define T3 tuple #define T4 tuple #define INF 1000000007 #define SIZE 400010 #define MOD 998244353 typedef long long LL; LL N; string S; LL V[SIZE]; int main() { cin >> N >> S; REP(i, N) cin >> V[i]; LL X = INF; mcf_graph graph(N + 2); string T = "yuki"; REP(k, 4) { REP(i, N) { if (S[i] == T[k]) { REP(j, N) { if (j <= i) continue; if (S[j] == T[k]) { graph.add_edge(i, j, SIZE, 0); break; } } } } } REP(i, N) { if (S[i] == 'y') { graph.add_edge(N, i, SIZE, 0); break; } } REP(k, 3) { REP(i, N) { if (S[i] == T[k]) { REP(j, N) { if (j <= i) continue; if (S[j] == T[k + 1]) { graph.add_edge(i, j, 1, X - V[i]); break; } } } } } REP(i, N) { if (S[i] == 'i') { graph.add_edge(i, N + 1, 1, X - V[i]); } } LP res = graph.flow(N, N + 1); cout << -(res.second - 4 * X * res.first) << endl; }