結果

問題 No.1288 yuki collection
ユーザー chocoruskchocorusk
提出日時 2020-09-25 03:24:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 5,000 ms
コード長 2,562 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 141,036 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 14:37:12
合計ジャッジ時間 6,147 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 127 ms
4,380 KB
testcase_14 AC 125 ms
4,376 KB
testcase_15 AC 100 ms
4,380 KB
testcase_16 AC 102 ms
4,384 KB
testcase_17 AC 128 ms
4,380 KB
testcase_18 AC 129 ms
4,380 KB
testcase_19 AC 129 ms
4,376 KB
testcase_20 AC 130 ms
4,380 KB
testcase_21 AC 123 ms
4,384 KB
testcase_22 AC 125 ms
4,380 KB
testcase_23 AC 124 ms
4,380 KB
testcase_24 AC 131 ms
4,380 KB
testcase_25 AC 129 ms
4,380 KB
testcase_26 AC 129 ms
4,376 KB
testcase_27 AC 56 ms
4,384 KB
testcase_28 AC 70 ms
4,384 KB
testcase_29 AC 57 ms
4,380 KB
testcase_30 AC 6 ms
4,384 KB
testcase_31 AC 7 ms
4,376 KB
testcase_32 AC 8 ms
4,376 KB
testcase_33 AC 108 ms
4,380 KB
testcase_34 AC 155 ms
4,384 KB
testcase_35 AC 148 ms
4,380 KB
testcase_36 AC 179 ms
4,380 KB
testcase_37 AC 172 ms
4,380 KB
testcase_38 AC 35 ms
4,376 KB
testcase_39 AC 35 ms
4,380 KB
testcase_40 AC 3 ms
4,384 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,380 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 MinCostFlow{
	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 n;
	vector<vector<edge>> g;
	vector<ll> h, dist;
	vector<int> prevv, preve;
	MinCostFlow(int n):n(n), g(n), h(n), dist(n), prevv(n), preve(n){}
	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 flow(int s, int t, int f){
		using P=pair<ll, int>;
		const ll INF=1e18;
		ll res=0;
		fill(h.begin(), h.end(), 0);
		while(f>0){
			priority_queue<P, vector<P>, greater<P>> que;
			fill(dist.begin(), dist.end(), 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<n; 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 s;
ll v[3030];
int main()
{
    cin>>n;
    cin>>s;
    for(int i=0; i<n; i++) cin>>v[i];
	MinCostFlow mcf(n+1);
	const ll inf=1e9;
	string s0="yuki";
	int idx[5];
	fill(idx, idx+5, n+1);
	idx[4]=n;
	for(int i=n-1; i>=0; i--){
		for(int k=0; k<4; k++){
			if(s0[k]!=s[i])continue;
			if(idx[k]<=n) mcf.add_edge(i, idx[k], n/4, 0);
			if(idx[k+1]<=n) mcf.add_edge(i, idx[k+1], 1, inf-v[i]);
			idx[k]=i;
		}
	}
	if(idx[0]==n+1){
		cout<<0<<endl;
		return 0;
	}
	mcf.add_edge(idx[0], n, n/4, inf*4);
    cout<<-mcf.flow(idx[0], n, n/4)+inf*4*(n/4)<<endl;
	return 0;
}
0