#その2 最初はコストが全部正の場合に使えるダイクストラ from heapq import heappush,heappop class MinCostFlow: inf = 10 ** 18 def __init__(self,N): self.N = N self.G = [[] for _ in range(N)] self.H = [0] * N self.edge = [] def add_edge(self,fr,to,cap,cost): e = [to,cap,cost,None] r = e[3] = [fr,0,-cost,e] self.G[fr].append(e) self.G[to].append(r) self.edge.append(e) def get_edge(self,i): return self.edge[i] def flow(self,s,t,f): N = self.N G = self.G inf = MinCostFlow.inf res = 0 H = self.H #ポテンシャル、コストが最初は正なので、最初は0でいい prev_v = [0] * N prev_e = [None] * N d0 = [inf] * N dist = [inf] * N while f: dist[:] = d0 dist[s] = 0 q = [(0,s)] while q: c,v = heappop(q) if dist[v] < c:continue r0 = dist[v] + H[v] for e in G[v]: w,cap,cost,_ = e if cap > 0 and r0 + cost - H[w] < dist[w]: dist[w] = r = r0 + cost - H[w] prev_v[w] = v prev_e[w] = e heappush(q,(r,w)) if dist[t] == inf: return None """ for i in range(N): H[i] += dist[i] """ H = [h + d for h,d in zip(H,dist)] d = f v = t while v != s: d = min(d,prev_e[v][1]) v = prev_v[v] f -= d res += d * H[t] v = t while v != s: e = prev_e[v] e[1] -= d e[3][1] += d v = prev_v[v] return res N = int(input()) S = input() V = list(map(int,input().split())) d = {"y":0,"u":1,"k":2,"i":3} dat = [[] for _ in range(4)] for i in range(N): dat[d[S[i]]].append(i) s = 0 t = N + 1 inf = 10 ** 10 mincost = MinCostFlow(N+2 + N) base = N + 2 for i in range(3): a = dat[i] b = dat[i+1] for x in a: for y in b: if y < x:continue mincost.add_edge(x+base,y+1,1,0) #print(x+1,y+1) for x in dat[-1]: mincost.add_edge(x+base,t,1,0) for x in dat[0]: mincost.add_edge(s,x+1,1,0) for x in range(N): mincost.add_edge(x+1,x + base,1,inf - V[x]) ans = 0 while True: tmp = mincost.flow(s,t,1) if tmp == None:break tmp = inf * 4 - tmp #print(tmp) ans += tmp print(ans) #print(dat)