結果
問題 | No.1288 yuki collection |
ユーザー | mkawa2 |
提出日時 | 2023-10-18 21:50:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,237 ms / 5,000 ms |
コード長 | 5,417 bytes |
コンパイル時間 | 174 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 98,720 KB |
最終ジャッジ日時 | 2024-09-18 17:53:27 |
合計ジャッジ時間 | 26,758 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 68 ms
69,120 KB |
testcase_01 | AC | 68 ms
68,608 KB |
testcase_02 | AC | 71 ms
69,248 KB |
testcase_03 | AC | 106 ms
77,804 KB |
testcase_04 | AC | 71 ms
68,864 KB |
testcase_05 | AC | 69 ms
68,608 KB |
testcase_06 | AC | 74 ms
70,272 KB |
testcase_07 | AC | 72 ms
68,608 KB |
testcase_08 | AC | 140 ms
78,720 KB |
testcase_09 | AC | 120 ms
77,952 KB |
testcase_10 | AC | 151 ms
79,104 KB |
testcase_11 | AC | 106 ms
78,464 KB |
testcase_12 | AC | 134 ms
78,592 KB |
testcase_13 | AC | 919 ms
91,488 KB |
testcase_14 | AC | 941 ms
93,208 KB |
testcase_15 | AC | 764 ms
88,716 KB |
testcase_16 | AC | 754 ms
89,076 KB |
testcase_17 | AC | 981 ms
93,388 KB |
testcase_18 | AC | 990 ms
93,192 KB |
testcase_19 | AC | 948 ms
92,780 KB |
testcase_20 | AC | 1,009 ms
94,240 KB |
testcase_21 | AC | 1,233 ms
97,628 KB |
testcase_22 | AC | 1,237 ms
97,548 KB |
testcase_23 | AC | 1,208 ms
97,256 KB |
testcase_24 | AC | 949 ms
93,296 KB |
testcase_25 | AC | 946 ms
92,368 KB |
testcase_26 | AC | 956 ms
92,872 KB |
testcase_27 | AC | 658 ms
88,044 KB |
testcase_28 | AC | 679 ms
89,320 KB |
testcase_29 | AC | 555 ms
87,024 KB |
testcase_30 | AC | 222 ms
81,416 KB |
testcase_31 | AC | 256 ms
82,196 KB |
testcase_32 | AC | 259 ms
82,768 KB |
testcase_33 | AC | 1,205 ms
98,720 KB |
testcase_34 | AC | 1,216 ms
98,024 KB |
testcase_35 | AC | 1,141 ms
96,976 KB |
testcase_36 | AC | 809 ms
90,864 KB |
testcase_37 | AC | 859 ms
90,964 KB |
testcase_38 | AC | 869 ms
90,948 KB |
testcase_39 | AC | 873 ms
90,432 KB |
testcase_40 | AC | 155 ms
80,672 KB |
testcase_41 | AC | 70 ms
68,864 KB |
testcase_42 | AC | 70 ms
68,736 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = -1-(-1 << 31) # inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 from typing import NamedTuple, Optional, List, Tuple, cast from heapq import heappush, heappop class MCFGraph: class Edge(NamedTuple): src: int dst: int cap: int flow: int cost: int class _Edge: def __init__(self, dst: int, cap: int, cost: int) -> None: self.dst = dst self.cap = cap self.cost = cost self.rev: Optional[MCFGraph._Edge] = None def __init__(self, n: int) -> None: self._n = n self._g: List[List[MCFGraph._Edge]] = [[] for _ in range(n)] self._edges: List[MCFGraph._Edge] = [] def add_edge(self, src: int, dst: int, cap: int, cost: int) -> int: assert 0 <= src < self._n assert 0 <= dst < self._n assert 0 <= cap m = len(self._edges) e = MCFGraph._Edge(dst, cap, cost) re = MCFGraph._Edge(src, 0, -cost) e.rev = re re.rev = e self._g[src].append(e) self._g[dst].append(re) self._edges.append(e) return m def get_edge(self, i: int) -> Edge: assert 0 <= i < len(self._edges) e = self._edges[i] re = cast(MCFGraph._Edge, e.rev) return MCFGraph.Edge( re.dst, e.dst, e.cap+re.cap, re.cap, e.cost ) def edges(self) -> List[Edge]: return [self.get_edge(i) for i in range(len(self._edges))] def flow(self, s: int, t: int, flow_limit: Optional[int] = None) -> Tuple[int, int]: return self.slope(s, t, flow_limit)[-1] def slope(self, s: int, t: int, flow_limit: Optional[int] = None) -> List[Tuple[int, int]]: assert 0 <= s < self._n assert 0 <= t < self._n assert s != t if flow_limit is None: flow_limit = cast(int, sum(e.cap for e in self._g[s])) dual = [0]*self._n prev: List[Optional[Tuple[int, MCFGraph._Edge]]] = [None]*self._n def refine_dual() -> bool: pq = [(0, s)] visited = [False]*self._n dist: List[Optional[int]] = [None]*self._n dist[s] = 0 while pq: dist_v, v = heappop(pq) if visited[v]: continue visited[v] = True if v == t: break dual_v = dual[v] for e in self._g[v]: w = e.dst if visited[w] or e.cap == 0: continue reduced_cost = e.cost-dual[w]+dual_v new_dist = dist_v+reduced_cost dist_w = dist[w] if dist_w is None or new_dist < dist_w: dist[w] = new_dist prev[w] = v, e heappush(pq, (new_dist, w)) else: return False dist_t = dist[t] for v in range(self._n): if visited[v]: dual[v] -= cast(int, dist_t)-cast(int, dist[v]) return True flow = 0 cost = 0 prev_cost_per_flow: Optional[int] = None result = [(flow, cost)] while flow < flow_limit: if not refine_dual(): break f = flow_limit-flow v = t while prev[v] is not None: u, e = cast(Tuple[int, MCFGraph._Edge], prev[v]) f = min(f, e.cap) v = u v = t while prev[v] is not None: u, e = cast(Tuple[int, MCFGraph._Edge], prev[v]) e.cap -= f assert e.rev is not None e.rev.cap += f v = u c = -dual[s] flow += f cost += f*c if c == prev_cost_per_flow: result.pop() result.append((flow, cost)) prev_cost_per_flow = c return result base=10**9 n = II() S = ["yuki".find(c) for c in SI()] vv = LI() mf = MCFGraph(n+2) s = n t = s+1 ii = [[], [], [], []] for i in range(n)[::-1]: c = S[i] if c == 3: mf.add_edge(i, t, 1, base-vv[i]) elif ii[c+1]: mf.add_edge(i, ii[c+1][-1], 1, base-vv[i]) ii[c].append(i) for c in range(4): for i, j in zip(ii[c], ii[c][1:]): mf.add_edge(j, i, n, 0) if ii[0]: mf.add_edge(s, ii[0][-1], n, 0) mf.add_edge(s, t, n, base*4) ans = mf.flow(s, t, n//4) # for e in mf.edges(): # print(e) # if e.flow:print(e.src,e.dst,-e.cost) # print(ans) print(n//4*base*4-ans[1])