import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = -1-(-1 << 31) # inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 from heapq import * class MinCostFlow: def __init__(self, n): self.inf=10**9 self.n = n self.to = [[] for _ in range(n)] def add_edge(self, u, v, cap, cost): self.to[u].append([v, cap, cost, len(self.to[v])]) self.to[v].append([u, 0, -cost, len(self.to[u])-1]) # s...source,t...sink,f...flow # これが本体 def cal(self,s,t,f): min_cost=0 n=self.n pot=[0]*n while f: dist = [self.inf] * n pre_u=[-1]*n # 最短距離の遷移元の頂点 pre_e=[-1]*n # 最短距離の遷移元の辺 # ダイクストラで最短距離を求める hp=[] heappush(hp,(0,s)) dist[s]=0 while hp: d,u=heappop(hp) if d>dist[u]:continue for i,(v,cap,cost,rev) in enumerate(self.to[u]): if cap==0:continue nd=dist[u]+cost+pot[u]-pot[v] if nd>=dist[v]:continue dist[v]=nd pre_u[v]=u pre_e[v]=i heappush(hp,(nd,v)) # sinkまで届かなかったら不可能ということ if dist[t]==self.inf:return -1 # ポテンシャルを更新する for u in range(n):pot[u]+=dist[u] # パスs-t上で最小の容量=流す量を求める u=t min_cap=f while u!=s: u,e=pre_u[u],pre_e[u] min_cap=min(min_cap,self.to[u][e][1]) # フローから流す量を減らし、コストを加える f-=min_cap min_cost+=min_cap*pot[t] # パスs-tの容量を更新する u=t while u!=s: u,e,v=pre_u[u],pre_e[u],u self.to[u][e][1]-=min_cap rev=self.to[u][e][3] self.to[v][rev][1]+=min_cap return min_cost n = II() S = ["yuki".find(c) for c in SI()] vv = LI() mf = MinCostFlow(n+2) s = n t = s+1 ii = [[], [], [], []] for i in range(n)[::-1]: c = S[i] if c == 3: mf.add_edge(i, t, 1, -vv[i]) elif ii[c+1]: mf.add_edge(i, ii[c+1][-1], 1, -vv[i]) ii[c].append(i) for c in range(4): for i, j in zip(ii[c], ii[c][1:]): mf.add_edge(j, i, n, 0) if ii[0]: mf.add_edge(s, ii[0][-1], n, 0) mf.add_edge(s,t,n//4,0) ans = mf.cal(s, t,n//4) print(-ans)