結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 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 102 ms
5,376 KB
testcase_14 AC 102 ms
5,376 KB
testcase_15 AC 81 ms
5,376 KB
testcase_16 AC 81 ms
5,376 KB
testcase_17 AC 104 ms
5,376 KB
testcase_18 AC 105 ms
5,376 KB
testcase_19 AC 105 ms
5,376 KB
testcase_20 AC 106 ms
5,376 KB
testcase_21 AC 97 ms
5,376 KB
testcase_22 AC 99 ms
5,376 KB
testcase_23 AC 97 ms
5,376 KB
testcase_24 AC 107 ms
5,376 KB
testcase_25 AC 104 ms
5,376 KB
testcase_26 AC 104 ms
5,376 KB
testcase_27 AC 47 ms
5,376 KB
testcase_28 AC 56 ms
5,376 KB
testcase_29 AC 47 ms
5,376 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 7 ms
5,376 KB
testcase_32 AC 7 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 T,typename E>
struct PrimalDual{
    const E inf=numeric_limits<E>::max();
    struct edge{
        int to,rev; T cap; E cost;
        edge(int to,T cap,E cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
    };
    vector<vector<edge>> G;
    vector<pair<int,int>> pos;
    vector<E> h,dist;
    vector<int> prevv,preve;
    PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}
    int add_edge(int from,int to,T cap,E cost){
        pos.emplace_back(from,G[from].size());
        G[from].emplace_back(to,cap,cost,G[to].size());
        G[to].emplace_back(from,0,-cost,G[from].size()-1);
        return pos.size()-1;
    }
    tuple<int,int,int,int,int> get_edge(int i){
        auto e=G[pos[i].first][pos[i].second];
        auto re=G[e.to][e.rev];
        return {pos[i].first,e.to,e.cap+re.cap,re.cap,e.cost};
    }
    vector<tuple<int,int,int,int,int>> edges(){
        vector<tuple<int,int,int,int,int>> res;
        for (int i=0;i<pos.size();++i){
            res.emplace_back(get_edge(i));
        }
        return res;
    }
    void dijkstra(int s){
        struct P{
            E c; int v;
            P(E c,int v):c(c),v(v){}
            bool operator<(const P &rhs) const {return c>rhs.c;}
        };
        priority_queue<P> pq;
        fill(dist.begin(),dist.end(),inf);
        dist[s]=0; pq.emplace(dist[s],s);
        while(!pq.empty()){
            auto p=pq.top(); pq.pop();
            int v=p.v;
            if (dist[v]<p.c) continue;
            for (int i=0;i<G[v].size();++i){
                auto &e=G[v][i];
                if (e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v; preve[e.to]=i;
                    pq.emplace(dist[e.to],e.to);
                }
            }
        }
    }
    vector<pair<T,E>> slope(int s,int t,T lim){
        T f=0; E c=0,pre=-1;
        vector<pair<T,E>> res;
        res.emplace_back(f,c);
        while(f<lim){
            dijkstra(s);
            if (dist[t]==inf) break;
            for (int v=0;v<G.size();++v) h[v]+=dist[v];
            T d=lim-f;
            for (int v=t;v!=s;v=prevv[v]){
                d=min(d,G[prevv[v]][preve[v]].cap);
            }
            for (int v=t;v!=s;v=prevv[v]){
                auto &e=G[prevv[v]][preve[v]];
                e.cap-=d; G[v][e.rev].cap+=d;
            }
            f+=d; c+=h[t]*d;
            if (pre==h[t]) res.pop_back();
            res.emplace_back(f,c);
            pre=c;
        }
        return res;
    }
    E min_cost_flow(int s,int t,T f){
        auto res=slope(s,t,f).back();
        return res.first==f?res.second:-1;
    }
    pair<T,E> min_cost_max_flow(int s,int t){
        return slope(s,t,numeric_limits<T>::max()).back();
    }
    vector<pair<T,E>> min_cost_slope(int s,int t){
        return slope(s,t,numeric_limits<T>::max());
    }
};

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.min_cost_flow(n,n+1,2010) << endl;
}

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