結果

問題 No.1288 yuki collection
ユーザー SHIJOUSHIJOU
提出日時 2021-01-02 02:22:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 3,324 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 216,716 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 20:07:40
合計ジャッジ時間 6,464 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 1 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 118 ms
5,376 KB
testcase_14 AC 119 ms
5,376 KB
testcase_15 AC 97 ms
5,376 KB
testcase_16 AC 97 ms
5,376 KB
testcase_17 AC 120 ms
5,376 KB
testcase_18 AC 123 ms
5,376 KB
testcase_19 AC 122 ms
5,376 KB
testcase_20 AC 119 ms
5,376 KB
testcase_21 AC 112 ms
5,376 KB
testcase_22 AC 117 ms
5,376 KB
testcase_23 AC 116 ms
5,376 KB
testcase_24 AC 129 ms
5,376 KB
testcase_25 AC 123 ms
5,376 KB
testcase_26 AC 130 ms
5,376 KB
testcase_27 AC 62 ms
5,376 KB
testcase_28 AC 68 ms
5,376 KB
testcase_29 AC 52 ms
5,376 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 7 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 99 ms
5,376 KB
testcase_34 AC 139 ms
5,376 KB
testcase_35 AC 133 ms
5,376 KB
testcase_36 AC 169 ms
5,376 KB
testcase_37 AC 161 ms
5,376 KB
testcase_38 AC 43 ms
5,376 KB
testcase_39 AC 44 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

template<class T>
struct MinCostFlow {
	struct edge {
		int to, cap;
		T cost;
		int rev;
	};
	using P = pair<T, int>;
	int V;
	vector<vector<edge>> G;
	vector<T> h, dist;
	vector<int> prevv, preve;
	const T INF;
	//INF*Fがオーバーフローしない かつ costより十分に大きい
	MinCostFlow(int n, T INF_):V(n), G(n), h(n), dist(n), prevv(n), preve(n), INF(INF_) {}

	inline void add(int from, int to, int cap, T cost) {
		G[from].emplace_back((edge){to, cap, cost, (int)G[to].size()});
		G[to].emplace_back((edge){from, 0, -cost, (int)G[from].size()-1});
	}
	//最小費用流//無理な時は-1
	T min_cost_flow(const int s, const int t, int f) {
		T res = 0;
		while(f > 0) {
			priority_queue<P, vector<P>, greater<P>> que;
			fill(dist.begin(), dist.end(), INF);
			dist[s] = 0;
			que.emplace(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 < (int)G[v].size(); i++) {
					edge &e = G[v][i];
					if(e.cap > 0 and 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.emplace(dist[e.to], e.to);
					}
				}
			}
			if(dist[t] == INF) return -1;
			for(int v = 0; v < V; v++) h[v] += dist[v];

			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;
	}
};

//head

#define MF 0x7fffffffLL
const string A = "yuki";

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	string s;
	cin >> s;
	vi v(n);
	rep(i, n) cin >> v[i];
	MinCostFlow<ll> mcf(n+2, LINF/100);
	const int st = n, en = st+1, f = n>>2;
	mcf.add(st, en, f, MF*4);
	vector<queue<int>> q(4);
	rep(i, n) {
		rep(id, 4) {
			if(A[id] == s[i]) {
				q[id].push(i);
			}
		}
	}
	rep(i, n) {
		int id = find(all(A), s[i])-A.begin();
		q[id].pop();
		if(!q[id].empty()) {
			mcf.add(i, q[id].front(), f, 0);
		}
		if(id == 0) {
			mcf.add(st, i, 1, 0);
		}
		if(id == 3) {
			mcf.add(i, en, 1, MF-v[i]);
		}
		else {
			if(!q[id+1].empty()) {
				mcf.add(i, q[id+1].front(), 1, MF-v[i]);
			}
		}
	}
	cout << MF*f*4-mcf.min_cost_flow(st, en, f) << endl;
}
0