#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) struct mcf{ static const int xN = 10000; template using nega_queue = priority_queue,greater>; struct Edge{ int u,v,r; LL c; LL d; }; int N; int fSrc=0; int fSnk=1; vector J; vector E[xN]; LL F; LL fAns = 0; LL fD[xN]; LL fPE[xN]; LL fP[xN]={}; void addEdge(int u,int v,LL c,LL d){ E[u].push_back(J.size()); J.push_back({u,v,(int)J.size()+1,c,d}); E[v].push_back(J.size()); J.push_back({v,u,(int)J.size()-1,0,-d}); } void fDijkstra(){ rep(i,N) fD[i]=fPE[i]=-1; nega_queue>> Q; Q.push({0,{fSrc,-1}}); while(Q.size()){ LL d=Q.top().first; int p=Q.top().second.first; LL pre=Q.top().second.second; Q.pop(); if(fD[p]!=-1) continue; fD[p]=d; fPE[p]=pre; for(int j:E[p]){ if(J[j].c==0)continue; LL nxd = d+J[j].d+fP[p]-fP[J[j].v]; if(fD[J[j].v] <= nxd) continue; Q.push({nxd,{J[j].v,j}}); } } rep(i,N) fP[i]+=fD[i]; } void flow(){ fDijkstra(); if(fD[fSnk]==-1){ F=0; fAns=-1; return; } int p=fSnk; LL c=F; while(p!=fSrc){ c=min(c,J[fPE[p]].c); p=J[fPE[p]].u; } p=fSnk; while(p!=fSrc){ J[fPE[p]].c-=c; J[J[fPE[p]].r].c+=c; fAns += (int)c * J[fPE[p]].d; p=J[fPE[p]].u; } F-=c; } } G; int main(){ int N; cin>>N; string S; cin>>S; vector S2(N); rep(i,N){ S2[i]=string("yuki").find(S[i]); } vector V(N); rep(i,N) cin>>V[i]; G.fSrc=N*2; G.fSnk=N*2+1; G.N = N*2+2; static const LL maxCost = 1000000000; int preP[4]; rep(i,4) preP[i]=-1; rep(i,N){ int ch = S2[i]; G.addEdge(i,i+N,1,maxCost-V[i]); if(preP[ch]!=-1) G.addEdge(preP[ch]+N,i+N,1000000,0); if(ch!=0) if(preP[ch-1]!=-1) G.addEdge(preP[ch-1]+N,i,1000000,0); if(ch==0) G.addEdge(G.fSrc,i,1,0); if(ch==3) G.addEdge(i+N,G.fSnk,1,0); preP[ch]=i; } LL ans=0; for(LL f=1; ; f++){ G.F=1; while(G.F) G.flow(); if(G.fAns==-1) break; ans = max(ans,maxCost*4*f-G.fAns); } cout<