import heapq class MinCostFlow: def __init__(self, n): self.n=n self.g=[[] for _ in range(n)] self.h=[0]*n def add_edge(self, frm, to, cap, cost): g=self.g g[frm].append((to, cap, cost, len(g[to]))) g[to].append((frm, 0, -cost, len(g[frm])-1)) def mincostflow(self, s, t, f): res=0 n=self.n g=self.g h=self.h INF=10**18 h=[0]*n prevv=[0]*n preve=[0]*n while f>0: que=[] dist=[INF]*n dist[s]=0 heapq.heappush(que, (0, s)) while len(que)>0: p=heapq.heappop(que) v=p[1] if dist[v]
0 and dist[to]>dist[v]+cost+h[v]-h[to]: dist[to]=dist[v]+cost+h[v]-h[to] prevv[to]=v preve[to]=i heapq.heappush(que, (dist[to], to)) for i, d in enumerate(dist): h[i]+=d if dist[t]==INF: return -1 d=f v=t while v!=s: d=min(d, g[prevv[v]][preve[v]][1]) v=prevv[v] f-=d res+=d*h[t] v=t def add(e, d): return (e[0], e[1]+d, e[2], e[3]) while v!=s: g[prevv[v]][preve[v]]=add(g[prevv[v]][preve[v]], -d) rev=g[prevv[v]][preve[v]][3] g[v][rev]=add(g[v][rev], d) v=prevv[v] return res n=int(input()) s=input() v=list(map(int, input().split())) start, end, S, T=n+1, n, n+2, n+3 mcf=MinCostFlow(n+4) mcf.add_edge(S, start, n//4, 0) mcf.add_edge(end, T, n//4, 0) ans=0 f=0 s0="yuki" idx=[n+1]*5 idx[4]=n for i in range(n-1, -1, -1): k=s0.find(s[i]) if idx[k]<=n: mcf.add_edge(i, idx[k], n//4, 0) if idx[k+1]<=n: mcf.add_edge(S, idx[k+1], 1, 0) mcf.add_edge(idx[k+1], i, 1, v[i]) mcf.add_edge(i, T, 1, 0) ans+=v[i] f+=1 idx[k]=i if idx[0]<=n: mcf.add_edge(start, idx[0], n//4, 0) mcf.add_edge(start, end, n//4, 0) ans-=mcf.mincostflow(S, T, n//4+f) print(ans)