#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a struct MinCostFlow { const cost_t INF; struct edge { int to; flow_t cap; cost_t cost; int rev; bool isrev; }; vector> graph; vector potential,min_cost; vector prevv,preve; MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {} void add_edge(int from,int to,flow_t cap,cost_t cost){ graph[from].emplace_back((edge){to,cap,cost,(int)graph[to].size(),false}); graph[to].emplace_back((edge){from,0,-cost,(int)graph[from].size()-1,true}); } cost_t min_cost_flow(int s,int t,flow_t f){ int V=(int)graph.size(); cost_t ret=0; using Pi = pair; priority_queue,greater> PQ; potential.assign(V,0); preve.assign(V,-1); prevv.assign(V,-1); while(f>0){ min_cost.assign(V,INF); PQ.emplace(0,s); min_cost[s]=0; while(!PQ.empty()){ cost_t d; int now; tie(d,now)=PQ.top(); PQ.pop(); if(min_cost[now]0&&min_cost[e.to]>nextCost){ min_cost[e.to]=nextCost; prevv[e.to]=now; preve[e.to]=i; PQ.emplace(min_cost[e.to],e.to); } } } if(min_cost[t]==INF) return -1; for(int v=0;v>N; string S; cin>>S; vector V(N); rep(i,N) cin>>V[i]; MinCostFlow mcf(N+2); int s=N; int t=N+1; vector> v(4); rep(i,N){ rep(j,4) if(S[i]==P[j]) v[j].push_back(i); } int M=N/4; for(int j=0;j<4;j++){ for(int i=0;i+1