結果
| 問題 |
No.1288 yuki collection
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-11-13 21:47:17 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 5,172 bytes |
| コンパイル時間 | 766 ms |
| コンパイル使用メモリ | 123,564 KB |
| 実行使用メモリ | 11,788 KB |
| 最終ジャッジ日時 | 2024-06-22 09:52:23 |
| 合計ジャッジ時間 | 43,854 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 TLE * 2 -- * 19 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
class MinCostFlow(wint, cint) {
int n, m;
int[][] g;
int[] zu;
wint[] capa0, capa, ex;
cint[] cost;
real[] pot;
bool[] vis;
int[] itr, lev;
this(int n) {
this.n = n; m = 0; g = new int[][n]; zu = null; capa = null; cost = null;
ex = new wint[n]; pot = new real[n]; pot[] = 0; vis = new bool[n]; itr = new int[n]; lev = new int[n];
}
void addEdge(int u, int v, wint w, cint c) {
g[u] ~= m; zu ~= v; capa0 ~= w; capa ~= w; cost ~= +c; ++m;
g[v] ~= m; zu ~= u; capa0 ~= 0; capa ~= 0; cost ~= -c; ++m;
}
wint augment(int u, int t, wint f) {
if (u == t) return f;
foreach (i; g[u][itr[u] .. $]) {
if (capa[i] > 0 && lev[u] < lev[zu[i]]) {
wint g = augment(zu[i], t, min(f, capa[i]));
if (g > 0) {
capa[i] -= g; capa[i ^ 1] += g; return g;
}
}
++itr[u];
}
return 0;
}
wint augment(int u, wint f) {
if (ex[u] < 0) {
wint g = min(f, -ex[u]); ex[u] += g; return g;
}
foreach (i; g[u][itr[u] .. $]) {
if (capa[i] > 0 && cost[i] + pot[u] - pot[zu[i]] < 0) {
wint g = augment(zu[i], min(f, capa[i]));
if (g > 0) {
capa[i] -= g; capa[i ^ 1] += g; return g;
}
}
++itr[u];
}
return 0;
}
wint dinic(int s, int t, wint f) {
wint tof = 0;
for (; tof < f; ) {
int[] q;
lev[] = -1;
for (lev[s] = 0, q ~= s; !q.empty; ) {
int u = q.front; q.popFront;
foreach (i; g[u]) {
int v = zu[i];
if (capa[i] > 0 && lev[v] == -1) {
lev[v] = lev[u] + 1; q ~= v;
}
}
}
if (lev[t] == -1) break;
itr[] = 0;
for (wint g; (g = augment(s, t, f - tof)) > 0; ) tof += g;
}
return tof;
}
void dfs(int u) {
if (vis[u]) return; vis[u] = true;
foreach (i; g[u]) if (capa[i] > 0 && cost[i] + pot[u] - pot[zu[i]] < 0) {
dfs(zu[i]);
}
}
cint solve() {
real eps = 0;
// cint solve(int s, int t, wint flow) {
// real eps = 1;
// ex[s] += flow;
// ex[t] -= flow;
foreach (i; 0 .. m) if (capa[i] > 0) chmax(eps, cast(real)(-cost[i]));
for (; eps * n >= 1; ) {
eps /= 4;
foreach (i; 0 .. m) if (capa[i] > 0 && cost[i] + pot[zu[i ^ 1]] - pot[zu[i]] < 0) {
ex[zu[i ^ 1]] -= capa[i]; ex[zu[i]] += capa[i]; capa[i ^ 1] += capa[i]; capa[i] = 0;
}
for (; ; ) {
vis[] = false; itr[] = 0;
foreach (u; 0 .. n) if (ex[u] > 0) dfs(u);
foreach (u; 0 .. n) if (vis[u]) pot[u] -= eps;
foreach (u; 0 .. n) for (wint f; ex[u] > 0 && (f = augment(u, ex[u])) > 0; ) ex[u] -= f;
if (ex.all!"a <= 0") break;
}
}
cint toc = 0;
foreach (i; 0 .. m) if (capa0[i] > capa[i]) toc += (capa0[i] - capa[i]) * cost[i];
return toc;
}
}
void main() {
try {
for (; ; ) {
const N = readInt();
const S = readToken();
auto V = new long[N];
foreach (i; 0 .. N) {
V[i] = readLong();
}
auto mcf = new MinCostFlow!(int, long)(N * 2 + 2);
mcf.addEdge(N * 2 + 1, N * 2, N, 0);
foreach (i; 0 .. N) {
if (S[i] == 'y') {
mcf.addEdge(N * 2, i * 2, N, 0);
}
if (S[i] == 'i') {
mcf.addEdge(i * 2 + 1, N * 2 + 1, N, 0);
}
mcf.addEdge(i * 2, i * 2 + 1, 1, -V[i]);
foreach (j; i + 1 .. N) {
if (S[i] == S[j]) {
debug {
writeln("? ", i, " ", j);
}
mcf.addEdge(i * 2, j * 2, N, 0);
break;
}
}
foreach (j; i + 1 .. N) {
if ((S[i] == 'y' && S[j] == 'u') || (S[i] == 'u' && S[j] == 'k') || (S[i] == 'k' && S[j] == 'i')) {
debug {
writeln("! ", i, " ", j);
}
mcf.addEdge(i * 2 + 1, j * 2, N, 0);
break;
}
}
}
const res = mcf.solve();
writeln(-res);
}
} catch (EOFException e) {
}
}