結果

問題 No.1288 yuki collection
ユーザー 👑 NachiaNachia
提出日時 2020-11-22 20:45:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,159 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 212,200 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-30 23:04:43
合計ジャッジ時間 4,116 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
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<class T>
using nega_queue = priority_queue<T,vector<T>,greater<T>>;

struct Edge{ int u,v,r; LL c; LL d; };

int N;
int fSrc=0;
int fSnk=1;
vector<Edge> J;
vector<int> 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<pair<LL,pair<int,LL>>> 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<int> S2(N); rep(i,N){ S2[i]=string("yuki").find(S[i]); }
  vector<LL> 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<<ans<<endl;

  return 0;
}
0