#include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector srtunq(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) {} #endif // MinCostFlow based on AtCoder Library, no namespace, no private variables, compatible with C++11 // Reference: // **NO NEGATIVE COST EDGES** template struct mcf_graph { mcf_graph() {} mcf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); assert(0 <= cost); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap, cost}); g[to].push_back(_edge{from, from_id, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector edges() { int m = int(pos.size()); std::vector result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair flow(int s, int t) { return flow(s, t, std::numeric_limits::max()); } std::pair flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector> slope(int s, int t) { return slope(s, t, std::numeric_limits::max()); } std::vector dual, dist; std::vector pv, pe; std::vector vis; bool _dual_ref(int s, int t) { std::fill(dist.begin(), dist.end(), std::numeric_limits::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; // dist[v] = shortest(s, v) + dual[s] - dual[v] // dist[v] >= 0 (all reduced cost are positive) // dist[v] <= (n-1)C for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; // |-dual[e.to] + dual[v]| <= (n-1)C // cost <= C - -(n-1)C + 0 = nC Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; // dual[v] = dual[v] - dist[t] + dist[v] // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v]) // = - shortest(s, t) + dual[t] + shortest(s, v) // = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C dual[v] -= dist[t] - dist[v]; } return true; } std::vector> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); // variants (C = maxcost): // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0 // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge dual.assign(_n, 0), dist.assign(_n, 0); pv.assign(_n, 0), pe.assign(_n, 0); vis.assign(_n, false); Cap flow = 0; Cost cost = 0, prev_cost_per_flow = -1; std::vector> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!_dual_ref(s, t)) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto& e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost_per_flow == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost_per_flow = d; } return result; } struct _edge { int to, rev; Cap cap; Cost cost; }; int _n; std::vector> pos; std::vector> g; }; // template class MinCostFlowDAG { public: using Cat = CapType; using Cot = CostType; using pti = pair; struct edge { int to, rev; Cat cap; Cot cost; }; const int V; const Cot inf; vector > G; vector h, dist; vector deg, ord, prevv, preve; MinCostFlowDAG(const int node_size) : V(node_size), inf(numeric_limits::max()), G(V), h(V, inf), dist(V), deg(V, 0), prevv(V), preve(V){} void add_edge(const int from, const int to, const Cat cap, const Cot cost){ if(cap == 0) return; G[from].push_back((edge){to, (int)G[to].size(), cap, cost}); G[to].push_back((edge){from, (int)G[from].size() - 1, 0, -cost}); ++deg[to]; } bool tsort(){ queue que; for(int i = 0; i < V; ++i){ if(deg[i] == 0) que.push(i); } while(!que.empty()){ const int p = que.front(); que.pop(); ord.push_back(p); for(auto& e : G[p]){ if(e.cap > 0 && --deg[e.to] == 0) que.push(e.to); } } return (*max_element(deg.begin(), deg.end()) == 0); } void calc_potential(const int s){ h[s] = 0; for(const int v : ord){ if(h[v] == inf) continue; for(const edge& e : G[v]){ if(e.cap > 0) h[e.to] = min(h[e.to], h[v] + e.cost); } } } void Dijkstra(const int s){ priority_queue,greater > que; fill(dist.begin(), dist.end(), inf); dist[s] = 0; que.push(pti(0, s)); while(!que.empty()){ pti p = que.top(); que.pop(); const int v = p.second; if(dist[v] < p.first) continue; for(int i = 0; i < (int)G[v].size(); ++i){ edge& e = G[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){ dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v, preve[e.to] = i; que.push(pti(dist[e.to], e.to)); } } } } void update(const int s, const int t, Cat& f, Cot& res){ for(int i = 0; i < V; i++){ if(dist[i] != inf) h[i] += dist[i]; } Cat d = f; for(int v = t; v != s; v = prevv[v]){ d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += h[t] * d; for(int v = t; v != s; v = prevv[v]){ edge& e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } Cot solve(const int s, const int t, Cat f){ if(!tsort()) assert(false); // not DAG calc_potential(s); Cot res = 0; while(f > 0){ Dijkstra(s); if(dist[t] == inf) return -inf; update(s, t, f, res); } return res; } }; constexpr int B = 501; int main() { int N; string S; cin >> N >> S; vector V(N); cin >> V; const int s = N * 5, t = s + 1; MinCostFlowDAG graph(t + 1); REP(d, 5) { REP(i, N - 1) { graph.add_edge(d * N + i, d * N + i + 1, N / 4, 0); } } graph.add_edge(s - 1, 0, N / 4, 0); REP(i, N) { int b = 0; if (S[i] == 'u') b = N * 1; if (S[i] == 'k') b = N * 2; if (S[i] == 'i') b = N * 3; int fr = b + i + N, to = b + i; graph.add_edge(s, fr, 1, 0); graph.add_edge(fr, to, 1, V[i]); graph.add_edge(to, t, 1, 0); } auto cost = graph.solve(s, t, N); cout << accumulate(ALL(V), 0LL) - cost << '\n'; }