#include #include using namespace std; using namespace atcoder; __attribute__((constructor)) void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); } int main() { int n; string s; cin >> n >> s; vector v(n); for (auto& vi : v) cin >> vi; mcf_graph g(n + 2); int a = n, b = n + 1; int Y = -1, U = -1, K = -1, I = -1; for (int i = n-1; i >= 0; --i) { if (s[i] == 'i') { g.add_edge(i, b, 1, INT_MAX - v[i]); if (I != -1) g.add_edge(i, I, INT_MAX, 0); I = i; } else if (s[i] == 'k') { if (K != -1) g.add_edge(i, K, INT_MAX, 0); if (I != -1) g.add_edge(i, I, 1, INT_MAX - v[i]); K = i; } else if (s[i] == 'u') { if (U != -1) g.add_edge(i, U, INT_MAX, 0); if (K != -1) g.add_edge(i, K, 1, INT_MAX - v[i]); U = i; } else if (s[i] == 'y') { if (Y != -1) g.add_edge(i, Y, INT_MAX, 0); if (U != -1) g.add_edge(i, U, 1, INT_MAX - v[i]); Y = i; } } if (Y != -1) g.add_edge(a, Y, INT_MAX, 0); auto [flow, cost] = g.flow(a, b); long long ans = -(cost - (long long)4 * INT_MAX * flow); cout << ans << '\n'; }