結果

問題 No.1288 yuki collection
ユーザー chocoruskchocorusk
提出日時 2020-08-14 05:14:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 908 ms / 5,000 ms
コード長 3,081 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 138,060 KB
実行使用メモリ 4,488 KB
最終ジャッジ日時 2023-09-10 14:34:44
合計ジャッジ時間 22,644 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 664 ms
4,376 KB
testcase_14 AC 706 ms
4,380 KB
testcase_15 AC 622 ms
4,380 KB
testcase_16 AC 589 ms
4,380 KB
testcase_17 AC 754 ms
4,376 KB
testcase_18 AC 772 ms
4,376 KB
testcase_19 AC 756 ms
4,376 KB
testcase_20 AC 746 ms
4,380 KB
testcase_21 AC 631 ms
4,380 KB
testcase_22 AC 589 ms
4,380 KB
testcase_23 AC 564 ms
4,376 KB
testcase_24 AC 742 ms
4,380 KB
testcase_25 AC 740 ms
4,376 KB
testcase_26 AC 718 ms
4,376 KB
testcase_27 AC 816 ms
4,380 KB
testcase_28 AC 908 ms
4,380 KB
testcase_29 AC 779 ms
4,436 KB
testcase_30 AC 731 ms
4,380 KB
testcase_31 AC 730 ms
4,380 KB
testcase_32 AC 753 ms
4,380 KB
testcase_33 AC 527 ms
4,436 KB
testcase_34 AC 736 ms
4,376 KB
testcase_35 AC 733 ms
4,488 KB
testcase_36 AC 675 ms
4,376 KB
testcase_37 AC 657 ms
4,392 KB
testcase_38 AC 534 ms
4,380 KB
testcase_39 AC 533 ms
4,432 KB
testcase_40 AC 676 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct edge{
    int to, cap, rev; ll cost;
    edge(int to, int cap, ll cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};
int V;
vector<edge> g[6060];
ll h[6060];
ll dist[6060];
int prevv[6060], preve[6060];
void add_edge(int from, int to, int cap, ll cost){
    edge e=edge(to, cap, cost, g[to].size());
    g[from].push_back(e);
    e=edge(from, 0, -cost, g[from].size()-1);
    g[to].push_back(e);
}
ll min_cost_flow(int s, int t, int f){
    using P=pair<ll, int>;
    const ll INF=1e18;
    ll res=0;
    fill(h, h+V, 0);
    while(f>0){
        priority_queue<P, vector<P>, greater<P>> que;
        fill(dist, dist+V, INF);
        dist[s]=0;
        que.push({0, 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<g[v].size(); i++){
                edge &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;
                    que.push({dist[e.to], e.to});
                }
            }
        }
        for(int v=0; v<V; v++) h[v]+=dist[v];
        if(dist[t]==INF) return -1;
        int d=f;
        for(int v=t; v!=s; v=prevv[v]){
            d=min(d, g[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[t];
        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;
        }
    }
    return res;
}
int n;
string str;
ll v[3030];
int main()
{
    cin>>n;
    cin>>str;
    for(int i=0; i<n; i++) cin>>v[i];
	vector<int> w[5];
	for(int i=0; i<n; i++){
		if(str[i]=='y') w[0].push_back(i);
		else if(str[i]=='u') w[1].push_back(i);
		else if(str[i]=='k') w[2].push_back(i);
		else w[3].push_back(i);
	}
	if(w[0].empty()){
		cout<<0<<endl;
		return 0;
	}
	int s=w[0][0], t=n;
	int S=n+1, T=n+2;
	w[4].push_back(t);
	V=n+3;
	add_edge(S, s, n/4, 0);
	add_edge(t, T, n/4, 0);
	ll ans=0;
	int f=0;
	for(int k=0; k<4; k++){
		for(int i=0; i<(int)w[k].size()-1; i++){
			add_edge(w[k][i], w[k][i+1], n/4, 0);
		}
		for(int i:w[k]){
			int j=lower_bound(w[k+1].begin(), w[k+1].end(), i)-w[k+1].begin();
			if(j<w[k+1].size()){
				add_edge(S, w[k+1][j], 1, 0);
				add_edge(w[k+1][j], i, 1, v[i]);
				add_edge(i, T, 1, 0);
				ans+=v[i];
				f++;
			}
		}
	}
	add_edge(s, t, n/4, 0);
	ans-=min_cost_flow(S, T, n/4+f);
    cout<<ans<<endl;
	return 0;
}
0