結果

問題 No.1288 yuki collection
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-11-13 23:03:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 5,000 ms
コード長 4,116 bytes
コンパイル時間 859 ms
コンパイル使用メモリ 106,408 KB
実行使用メモリ 22,212 KB
最終ジャッジ日時 2023-09-30 04:01:35
合計ジャッジ時間 6,967 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
19,952 KB
testcase_01 AC 3 ms
19,828 KB
testcase_02 AC 4 ms
19,972 KB
testcase_03 AC 4 ms
19,888 KB
testcase_04 AC 3 ms
19,884 KB
testcase_05 AC 4 ms
19,932 KB
testcase_06 AC 3 ms
20,108 KB
testcase_07 AC 4 ms
19,824 KB
testcase_08 AC 4 ms
19,960 KB
testcase_09 AC 4 ms
20,008 KB
testcase_10 AC 4 ms
19,868 KB
testcase_11 AC 4 ms
19,848 KB
testcase_12 AC 4 ms
19,820 KB
testcase_13 AC 217 ms
22,100 KB
testcase_14 AC 209 ms
22,088 KB
testcase_15 AC 172 ms
22,172 KB
testcase_16 AC 171 ms
21,988 KB
testcase_17 AC 211 ms
21,956 KB
testcase_18 AC 218 ms
22,084 KB
testcase_19 AC 219 ms
22,012 KB
testcase_20 AC 219 ms
21,960 KB
testcase_21 AC 203 ms
22,032 KB
testcase_22 AC 205 ms
22,016 KB
testcase_23 AC 206 ms
22,208 KB
testcase_24 AC 224 ms
22,028 KB
testcase_25 AC 212 ms
21,948 KB
testcase_26 AC 213 ms
22,020 KB
testcase_27 AC 115 ms
22,008 KB
testcase_28 AC 118 ms
22,024 KB
testcase_29 AC 93 ms
22,000 KB
testcase_30 AC 11 ms
21,984 KB
testcase_31 AC 13 ms
21,988 KB
testcase_32 AC 15 ms
22,000 KB
testcase_33 AC 187 ms
21,980 KB
testcase_34 AC 250 ms
22,020 KB
testcase_35 AC 232 ms
22,196 KB
testcase_36 AC 258 ms
21,972 KB
testcase_37 AC 246 ms
22,016 KB
testcase_38 AC 98 ms
21,960 KB
testcase_39 AC 97 ms
22,092 KB
testcase_40 AC 6 ms
22,212 KB
testcase_41 AC 4 ms
19,888 KB
testcase_42 AC 5 ms
20,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


namespace MCF {
	#define MAXN 1000010
	#define MAXM 1000010
	#define wint int
	#define cint Int
	const wint wEPS = 0;
	const wint wINF = 1001001001;
	const cint cEPS = 0;
	const cint cINF = 1001001001001001001LL;
	int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
	wint capa[MAXM], tof;
	cint cost[MAXM], toc, d[MAXN], pot[MAXN];
	int vis[MAXN], pree[MAXN];
	void init(int _n) {
		n = _n; m = 0; memset(ptr, ~0, n << 2);
	}
	void ae(int u, int v, wint w, cint c) {
		next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = w; cost[m] = +c; ++m;
		next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -c; ++m;
	}
	bool solve(int src, int ink, wint flo = wINF) {
		wint f;
		cint c, cc;
		int i, u, v;
		memset(pot, 0, n * sizeof(cint));
	//*
		for (bool cont = 1; cont; ) {
			cont = 0;
			for (u = 0; u < n; ++u) for (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {
				if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
					pot[zu[i]] = pot[u] + cost[i]; cont = 1;
				}
			}
		}
	//*/
		for (toc = 0, tof = 0; tof + wEPS < flo; ) {
			typedef pair<cint,int> node;
			priority_queue< node,vector<node>,greater<node> > q;
			for (u = 0; u < n; ++u) { d[u] = cINF; vis[u] = 0; }
			for (q.push(make_pair(d[src] = 0, src)); !q.empty(); ) {
				c = q.top().first; u = q.top().second; q.pop();
				if (vis[u]++) continue;
				for (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {
					cc = c + cost[i] + pot[u] - pot[v = zu[i]];
					if (d[v] > cc) { q.push(make_pair(d[v] = cc, v)); pree[v] = i; }
				}
			}
			if (!vis[ink]) return 0;
			f = flo - tof;
			for (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; chmin(f, capa[i]); }
			for (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; capa[i] -= f; capa[i ^ 1] += f; }
			tof += f;
			toc += f * (d[ink] - pot[src] + pot[ink]);
			for (u = 0; u < n; ++u) pot[u] += d[u];
		}
		return 1;
	}
}


int N;
char S[2020];
Int V[2020];

int main() {
  for (; ~scanf("%d", &N); ) {
    scanf("%s", S);
    for (int i = 0; i < N; ++i) {
      scanf("%lld", &V[i]);
    }
    
    MCF::init(N * 2 + 2);
vector<int>ids(N);
    for (int i = 0; i < N; ++i) {
      if (S[i] == 'y') {
        MCF::ae(N * 2, i * 2, N, 0);
      }
      if (S[i] == 'i') {
        MCF::ae(i * 2 + 1, N * 2 + 1, N, 0);
      }
ids[i]=MCF::m;
      MCF::ae(i * 2, i * 2 + 1, 1, -V[i]);
      for (int j = i + 1; j < N; ++j) {
        if (S[i] == S[j]) {
          MCF::ae(i * 2, j * 2, N, 0);
          break;
        }
      }
      for (int j = i + 1; j < N; ++j) {
        if ((S[i] == 'y' && S[j] == 'u') || (S[i] == 'u' && S[j] == 'k') || (S[i] == 'k' && S[j] == 'i')) {
          MCF::ae(i * 2 + 1, j * 2, N, 0);
          break;
        }
      }
    }
    
    MCF::ae(N * 2, N * 2 + 1, N, 0);
    MCF::solve(N * 2, N * 2 + 1, N);
    printf("%lld\n", -MCF::toc);
    
vector<int>used(N,0);
for(int i=0;i<N;++i)used[i]=MCF::capa[ids[i]^1];
// cerr<<"used = ";pv(used.begin(),used.end());
Int score=0;
for(int i=0;i<N;++i)if(used[i])score+=V[i];
assert(-MCF::toc==score);
for(int i=0;i<N;++i)if(used[i]){
 int j=i;
 for(char c:{'y','u','k','i'}){
  for(;j<N&&!(S[j]==c&&used[j]);++j){}
  assert(j<N);
  used[j++]=0;
 }
}
for(int i=0;i<N;++i)assert(!used[i]);
  }
  return 0;
}
0