結果
問題 | No.1288 yuki collection |
ユーザー | chocorusk |
提出日時 | 2020-09-21 23:00:20 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 4,220 ms / 5,000 ms |
コード長 | 3,144 bytes |
コンパイル時間 | 167 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 95,796 KB |
最終ジャッジ日時 | 2024-06-28 05:15:10 |
合計ジャッジ時間 | 103,058 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
59,264 KB |
testcase_01 | AC | 44 ms
52,864 KB |
testcase_02 | AC | 61 ms
62,848 KB |
testcase_03 | AC | 124 ms
76,320 KB |
testcase_04 | AC | 44 ms
52,992 KB |
testcase_05 | AC | 52 ms
59,392 KB |
testcase_06 | AC | 113 ms
76,160 KB |
testcase_07 | AC | 60 ms
63,104 KB |
testcase_08 | AC | 121 ms
76,220 KB |
testcase_09 | AC | 117 ms
76,032 KB |
testcase_10 | AC | 135 ms
76,232 KB |
testcase_11 | AC | 119 ms
76,556 KB |
testcase_12 | AC | 121 ms
76,160 KB |
testcase_13 | AC | 3,724 ms
92,416 KB |
testcase_14 | AC | 3,603 ms
92,012 KB |
testcase_15 | AC | 3,009 ms
88,404 KB |
testcase_16 | AC | 2,951 ms
88,380 KB |
testcase_17 | AC | 3,923 ms
93,544 KB |
testcase_18 | AC | 3,734 ms
91,916 KB |
testcase_19 | AC | 3,633 ms
92,808 KB |
testcase_20 | AC | 3,754 ms
92,908 KB |
testcase_21 | AC | 3,734 ms
91,900 KB |
testcase_22 | AC | 3,261 ms
92,284 KB |
testcase_23 | AC | 3,450 ms
91,740 KB |
testcase_24 | AC | 3,560 ms
92,572 KB |
testcase_25 | AC | 3,665 ms
92,668 KB |
testcase_26 | AC | 3,716 ms
92,408 KB |
testcase_27 | AC | 4,157 ms
95,320 KB |
testcase_28 | AC | 4,220 ms
95,796 KB |
testcase_29 | AC | 3,596 ms
91,248 KB |
testcase_30 | AC | 4,045 ms
95,680 KB |
testcase_31 | AC | 3,945 ms
95,152 KB |
testcase_32 | AC | 3,887 ms
94,440 KB |
testcase_33 | AC | 3,040 ms
93,588 KB |
testcase_34 | AC | 3,510 ms
91,960 KB |
testcase_35 | AC | 3,670 ms
93,956 KB |
testcase_36 | AC | 3,180 ms
93,088 KB |
testcase_37 | AC | 3,507 ms
92,820 KB |
testcase_38 | AC | 2,954 ms
91,268 KB |
testcase_39 | AC | 3,053 ms
93,300 KB |
testcase_40 | AC | 3,648 ms
93,660 KB |
testcase_41 | AC | 44 ms
52,864 KB |
testcase_42 | AC | 44 ms
52,736 KB |
ソースコード
import heapq class MinCostFlow: def __init__(self, n, neg=False): self.n=n self.g=[[] for _ in range(n)] self.h=[0]*n self.neg=neg def add_edge(self, frm, to, cap, cost): g=self.g g[frm].append([to, cap, cost, len(g[to])]) g[to].append([frm, 0, -cost, len(g[frm])-1]) def bellman_ford(self, s, t, prevv, preve): n=self.n g=self.g h=self.h INF=10**18 for x in range(n): h[x]=INF h[s]=0 for _ in range(n-1): for x in range(n): for i, e in enumerate(g[x]): if e[1]==0: continue to, cost=e[0], e[2] if h[to]>h[x]+cost: h[to]=h[x]+cost prevv[to]=x preve[to]=i def flow(self, s, t, f): res=0 n=self.n g=self.g h=self.h INF=10**18 LOG=13 #n<2**LOG prevv=[0]*n preve=[0]*n while f>0: if self.neg: self.neg=False self.bellman_ford(s, t, prevv, preve) if h[t]==INF: return -1 else: que=[] dist=[INF]*n dist[s]=0 heapq.heappush(que, s) while que: p=heapq.heappop(que) v=(p&((1<<LOG)-1)) if dist[v]<(p>>LOG): continue for i, e in enumerate(g[v]): if e[1]==0: continue to, cost=e[0], e[2] if dist[to]>dist[v]+cost+h[v]-h[to]: dist[to]=dist[v]+cost+h[v]-h[to] prevv[to]=v preve[to]=i heapq.heappush(que, (dist[to]<<LOG)^to) for i, d in enumerate(dist): h[i]+=d if dist[t]==INF: return -1 d=f v=t while v!=s: d=min(d, g[prevv[v]][preve[v]][1]) v=prevv[v] f-=d res+=d*h[t] v=t while v!=s: g[prevv[v]][preve[v]][1]-=d rev=g[prevv[v]][preve[v]][3] g[v][rev][1]+=d v=prevv[v] return res n=int(input()) s=input() v=list(map(int, input().split())) start, end, S, T=n+1, n, n+2, n+3 mcf=MinCostFlow(n+4) mcf.add_edge(S, start, n//4, 0) mcf.add_edge(end, T, n//4, 0) ans=0 f=0 s0="yuki" idx=[n+1]*5 idx[4]=n for i in range(n-1, -1, -1): k=s0.find(s[i]) if idx[k]<=n: mcf.add_edge(i, idx[k], n//4, 0) if idx[k+1]<=n: mcf.add_edge(S, idx[k+1], 1, 0) mcf.add_edge(idx[k+1], i, 1, v[i]) mcf.add_edge(i, T, 1, 0) ans+=v[i] f+=1 idx[k]=i if idx[0]<=n: mcf.add_edge(start, idx[0], n//4, 0) mcf.add_edge(start, end, n//4, 0) ans-=mcf.flow(S, T, n//4+f) print(ans)