結果

問題 No.1288 yuki collection
ユーザー ChanyuhChanyuh
提出日時 2020-12-30 17:57:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,520 bytes
コンパイル時間 1,963 ms
コンパイル使用メモリ 141,332 KB
実行使用メモリ 10,656 KB
最終ジャッジ日時 2024-04-16 19:02:27
合計ジャッジ時間 10,522 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

template<typename TF,typename TC>
struct PrimalDual{
  struct edge{
    int to;
    TF cap;
    TC cost;
    int rev;
    edge(){}
    edge(int to,TF cap,TC cost,int rev):
      to(to),cap(cap),cost(cost),rev(rev){}
  };

  static const TC INF;
  vector<vector<edge>> G;
  vector<TC> h,dist;
  vector<int> prevv,preve;

  PrimalDual(){}
  PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}

  void add_edge(int u,int v,TF cap,TC cost){
    G[u].emplace_back(v,cap,cost,G[v].size());
    G[v].emplace_back(u,0,-cost,G[u].size()-1);
  }

  void dijkstra(int s){
    struct P{
      TC first;
      int second;
      P(TC first,int second):first(first),second(second){}
      bool operator<(const P&a) const{return a.first<first;}
    };
    priority_queue<P> que;
    fill(dist.begin(),dist.end(),INF);

    dist[s]=0;
    que.emplace(dist[s],s);
    while(!que.empty()){
      P p=que.top();que.pop();
      int v=p.second;
      if(dist[v]<p.first) continue;
      for(int i=0;i<(int)G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap==0) continue;
        if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){
          dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
          prevv[e.to]=v;
          preve[e.to]=i;
          que.emplace(dist[e.to],e.to);
        }
      }
    }
  }

  TC flow(int s,int t,TF f,int &ok){
    TC res=0;
    fill(h.begin(),h.end(),0);
    while(f>0){
      dijkstra(s);
      if(dist[t]==INF){
        ok=0;
        return res;
      }

      for(int v=0;v<(int)h.size();v++)
        if(dist[v]<INF) h[v]=h[v]+dist[v];

      TF d=f;
      for(int v=t;v!=s;v=prevv[v])
        d=min(d,G[prevv[v]][preve[v]].cap);

      f-=d;
      res=res+h[t]*d;
      for(int v=t;v!=s;v=prevv[v]){
        edge &e=G[prevv[v]][preve[v]];
        e.cap-=d;
        G[v][e.rev].cap+=d;
      }
    }
    ok=1;
    return res;
  }
};
template<typename TF, typename TC>
const TC PrimalDual<TF, TC>::INF = numeric_limits<TC>::max()/2;

int n;
string S;
ll V[2010];
vector<int> cs[4];

void solve(){
    cin >> n;
    cin >> S;
    rep(i,n) cin >> V[i];
    rep(i,n){
        if(S[i]=='y') cs[0].push_back(i);
        if(S[i]=='u') cs[1].push_back(i);
        if(S[i]=='k') cs[2].push_back(i);
        if(S[i]=='i') cs[3].push_back(i);
    }
    rep(i,4) if(cs[i].empty()){
        cout << 0 << endl;
        return;
    }
    PrimalDual<int,ll> mcf(n+2);
    int pos[4]={0,0,0,0};
    mcf.add_edge(n,cs[0][0],4010,0);
    rep(i,4){
        rep(j,cs[i].size()-1){
            mcf.add_edge(cs[i][j],cs[i][j+1],4010,0);
        }
    }
    rep(i,n){
        if(S[i]=='y'){
            pos[0]+=1;
            if(pos[1]>=0 && cs[1].size()>pos[1]) {
                mcf.add_edge(i,cs[1][pos[1]],1,-V[i]);
            }
        }
        if(S[i]=='u'){
            pos[1]+=1;
            if(pos[2]>=0 && cs[2].size()>pos[2]) mcf.add_edge(i,cs[2][pos[2]],1,-V[i]);
        }
        if(S[i]=='k'){
            pos[2]+=1;
            if(pos[3]>=0 && cs[3].size()>pos[3]) mcf.add_edge(i,cs[3][pos[3]],1,-V[i]);
        }
        if(S[i]=='i'){
            pos[3]+=1;
            mcf.add_edge(i,n+1,1,-V[i]);
        }
    }
    mcf.add_edge(n,n+1,4010,0);
    int ok;
    //cout << 1 << endl;
    cout << -mcf.flow(n,n+1,2010,ok) << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0