結果

問題 No.1288 yuki collection
ユーザー 👑 NachiaNachia
提出日時 2020-11-22 20:47:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 5,000 ms
コード長 2,158 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 212,100 KB
実行使用メモリ 4,416 KB
最終ジャッジ日時 2023-09-30 23:04:53
合計ジャッジ時間 9,934 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 272 ms
4,380 KB
testcase_14 AC 267 ms
4,380 KB
testcase_15 AC 219 ms
4,380 KB
testcase_16 AC 213 ms
4,380 KB
testcase_17 AC 273 ms
4,380 KB
testcase_18 AC 275 ms
4,376 KB
testcase_19 AC 274 ms
4,380 KB
testcase_20 AC 275 ms
4,376 KB
testcase_21 AC 274 ms
4,376 KB
testcase_22 AC 274 ms
4,376 KB
testcase_23 AC 276 ms
4,380 KB
testcase_24 AC 280 ms
4,380 KB
testcase_25 AC 271 ms
4,380 KB
testcase_26 AC 276 ms
4,380 KB
testcase_27 AC 137 ms
4,376 KB
testcase_28 AC 153 ms
4,416 KB
testcase_29 AC 137 ms
4,376 KB
testcase_30 AC 11 ms
4,376 KB
testcase_31 AC 15 ms
4,380 KB
testcase_32 AC 16 ms
4,376 KB
testcase_33 AC 217 ms
4,384 KB
testcase_34 AC 278 ms
4,380 KB
testcase_35 AC 273 ms
4,380 KB
testcase_36 AC 354 ms
4,380 KB
testcase_37 AC 344 ms
4,376 KB
testcase_38 AC 247 ms
4,380 KB
testcase_39 AC 226 ms
4,380 KB
testcase_40 AC 4 ms
4,376 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] != -1) 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