#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct PrimalDual { struct Edge { int dst, rev; T cap; U cost; Edge(int dst, T cap, U cost, int rev) : dst(dst), cap(cap), cost(cost), rev(rev) {} }; std::vector> graph; PrimalDual(int n, const T TINF, const U UINF) : n(n), TINF(TINF), UINF(UINF), graph(n), prev_v(n, -1), prev_e(n, -1), potential(n, 0), dist(n) {} void add_edge(int src, int dst, T cap, U cost) { has_negative_edge |= cost < 0; graph[src].emplace_back(dst, cap, cost, graph[dst].size()); graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1); } U minimum_cost_flow(int s, int t, T flow) { U res = 0; if (has_negative_edge) { bellman_ford(s); if (dist[t] == UINF) return UINF; res += calc(s, t, flow); } while (flow > 0) { dijkstra(s); if (dist[t] == UINF) return UINF; res += calc(s, t, flow); } return res; } U minimum_cost_flow(int s, int t) { U res = 0; bellman_ford(s); if (potential[t] >= 0 || dist[t] == UINF) return res; T tmp = TINF; res += calc(s, t, tmp); while (true) { dijkstra(s); if (potential[t] >= 0 || dist[t] == UINF) return res; res += calc(s, t, tmp); } } std::pair min_cost_max_flow(int s, int t, T flow) { T mx = flow; U cost = 0; if (has_negative_edge) { bellman_ford(s); if (dist[t] == UINF) return {mx - flow, cost}; cost += calc(s, t, flow); } while (flow > 0) { dijkstra(s); if (dist[t] == UINF) return {mx - flow, cost}; cost += calc(s, t, flow); } return {mx - flow, cost}; } private: using Pui = std::pair; int n; const T TINF; const U UINF; bool has_negative_edge = false; std::vector prev_v, prev_e; std::vector potential, dist; std::priority_queue, std::greater> que; void bellman_ford(int s) { std::fill(dist.begin(), dist.end(), UINF); dist[s] = 0; bool is_updated = true; for (int step = 0; step < n; ++step) { is_updated = false; for (int i = 0; i < n; ++i) { if (dist[i] == UINF) continue; for (int j = 0; j < graph[i].size(); ++j) { Edge e = graph[i][j]; if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) { dist[e.dst] = dist[i] + e.cost; prev_v[e.dst] = i; prev_e[e.dst] = j; is_updated = true; } } } if (!is_updated) break; } assert(!is_updated); for (int i = 0; i < n; ++i) { if (dist[i] != UINF) potential[i] += dist[i]; } } void dijkstra(int s) { std::fill(dist.begin(), dist.end(), UINF); dist[s] = 0; que.emplace(0, s); while (!que.empty()) { Pui pr = que.top(); que.pop(); int ver = pr.second; if (dist[ver] < pr.first) continue; for (int i = 0; i < graph[ver].size(); ++i) { Edge e = graph[ver][i]; U nx = dist[ver] + e.cost + potential[ver] - potential[e.dst]; if (e.cap > 0 && dist[e.dst] > nx) { dist[e.dst] = nx; prev_v[e.dst] = ver; prev_e[e.dst] = i; que.emplace(dist[e.dst], e.dst); } } } for (int i = 0; i < n; ++i) { if (dist[i] != UINF) potential[i] += dist[i]; } } U calc(int s, int t, T &flow) { T f = flow; for (int v = t; v != s; v = prev_v[v]) f = std::min(f, graph[prev_v[v]][prev_e[v]].cap); flow -= f; for (int v = t; v != s; v = prev_v[v]) { Edge &e = graph[prev_v[v]][prev_e[v]]; e.cap -= f; graph[v][e.rev].cap += f; } return potential[t] * f; } }; int main() { int n; string s; cin >> n >> s; vector a(n, -1); REP(i, n) { if (s[i] == 'y') { a[i] = 0; } else if (s[i] == 'u') { a[i] = 1; } else if (s[i] == 'k') { a[i] = 2; } else if (s[i] == 'i') { a[i] = 3; } } vector v(n); REP(i, n) cin >> v[i]; PrimalDual pd(n + 2, INF, LINF); const int s_node = n, t_node = n + 1; REP(i, n) { if (a[i] == 0) { pd.add_edge(s_node, i, INF, 0); break; } } REP(i, n) { if (a[i] == 3) { pd.add_edge(i, t_node, 1, -v[i]); } else { FOR(j, i + 1, n) { if (a[j] == a[i] + 1) { pd.add_edge(i, j, 1, -v[i]); break; } } } FOR(j, i + 1, n) { if (a[j] == a[i]) { pd.add_edge(i, j, INF, 0); break; } } } cout << -pd.minimum_cost_flow(s_node, t_node) << '\n'; return 0; }