#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } template struct PrimalDual{ struct edge{ int to; flow_t cap; cost_t cost; int rev;//この辺の逆辺がg[from]の何番目にあるか bool isrev; }; vector> graph; vector potential,min_cost; vector prevv,preve;//点,辺 const cost_t TINF; PrimalDual(int V):graph(V),TINF(numeric_limits::max()){} void add_edge(int from,int to,flow_t cap,cost_t cost){ graph[from].push_back((edge){to,cap,cost,(int)graph[to].size(),false}); graph[to].push_back((edge){from,0,-cost,(int)graph[from].size()-1,true}); } cost_t min_cost_flow(int s,int t,flow_t f,bool &ok){ int V=(int)graph.size(); cost_t ret=0; using Pi=pair; priority_queue,greater> que; potential.assign(V,0); preve.assign(V,-1); prevv.assign(V,-1); while(f>0){ min_cost.assign(V,TINF); que.emplace(0,s); min_cost[s]=0; //dijkstraパート while(!que.empty()){ Pi p=que.top();que.pop(); if(min_cost[p.second]0 and min_cost[e.to]>nextCost){ min_cost[e.to]=nextCost; prevv[e.to]=p.second,preve[e.to]=i; que.emplace(min_cost[e.to],e.to); } } } if(min_cost[t]==TINF){ ok=false; return ret; } // dijkstraの結果に応じてpotentialを調節 for(int v=0;v"<>n; string s;cin>>s; vector v(n); cin>>v; PrimalDual flow(n+2); int src=n,sink=n+1; rep(i,n){ if(s[i]=='y') flow.add_edge(src,i,1,0); if(s[i]=='i') flow.add_edge(i,sink,1,-v[i]); for(int j=i+1;j