結果

問題 No.1288 yuki collection
ユーザー chocoruskchocorusk
提出日時 2020-09-07 21:23:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,869 ms / 5,000 ms
コード長 2,362 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 130,564 KB
最終ジャッジ日時 2023-09-10 13:40:38
合計ジャッジ時間 117,645 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
75,484 KB
testcase_01 AC 76 ms
71,348 KB
testcase_02 AC 89 ms
76,328 KB
testcase_03 AC 142 ms
77,800 KB
testcase_04 AC 78 ms
71,392 KB
testcase_05 AC 82 ms
75,444 KB
testcase_06 AC 132 ms
77,760 KB
testcase_07 AC 88 ms
76,240 KB
testcase_08 AC 141 ms
77,864 KB
testcase_09 AC 139 ms
77,612 KB
testcase_10 AC 151 ms
77,840 KB
testcase_11 AC 138 ms
77,804 KB
testcase_12 AC 144 ms
77,752 KB
testcase_13 AC 4,078 ms
101,804 KB
testcase_14 AC 3,925 ms
100,456 KB
testcase_15 AC 3,361 ms
96,152 KB
testcase_16 AC 3,273 ms
96,760 KB
testcase_17 AC 4,320 ms
103,236 KB
testcase_18 AC 4,140 ms
101,072 KB
testcase_19 AC 3,912 ms
102,144 KB
testcase_20 AC 4,106 ms
103,456 KB
testcase_21 AC 3,886 ms
113,724 KB
testcase_22 AC 3,877 ms
114,460 KB
testcase_23 AC 4,041 ms
112,708 KB
testcase_24 AC 3,937 ms
101,780 KB
testcase_25 AC 4,079 ms
102,128 KB
testcase_26 AC 4,137 ms
102,504 KB
testcase_27 AC 4,869 ms
127,288 KB
testcase_28 AC 4,785 ms
118,716 KB
testcase_29 AC 3,861 ms
97,904 KB
testcase_30 AC 4,574 ms
130,564 KB
testcase_31 AC 4,458 ms
128,544 KB
testcase_32 AC 4,546 ms
128,672 KB
testcase_33 AC 3,579 ms
111,816 KB
testcase_34 AC 3,752 ms
98,964 KB
testcase_35 AC 4,176 ms
104,236 KB
testcase_36 AC 3,639 ms
99,056 KB
testcase_37 AC 4,136 ms
99,536 KB
testcase_38 AC 3,358 ms
108,276 KB
testcase_39 AC 3,443 ms
111,720 KB
testcase_40 AC 4,185 ms
123,760 KB
testcase_41 AC 77 ms
71,424 KB
testcase_42 AC 76 ms
71,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class MinCostFlow:
    def __init__(self, n):
        self.n=n
        self.g=[[] for _ in range(n)]
        self.h=[0]*n
    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 mincostflow(self, s, t, f):
        res=0
        n=self.n
        g=self.g
        h=self.h
        INF=10**18
        h=[0]*n
        prevv=[0]*n
        preve=[0]*n
        while f>0:
            que=[]
            dist=[INF]*n
            dist[s]=0
            heapq.heappush(que, s)
            while len(que)>0:
                p=heapq.heappop(que)
                v=(p&((1<<20)-1))
                if dist[v]<(p>>20):
                    continue
                for i, e in enumerate(g[v]):
                    to, cost=e[0], e[2]
                    if e[1]>0 and 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]<<20)^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
            def add(e, d):
                return (e[0], e[1]+d, e[2], e[3])
            while v!=s:
                g[prevv[v]][preve[v]]=add(g[prevv[v]][preve[v]], -d)
                rev=g[prevv[v]][preve[v]][3]
                g[v][rev]=add(g[v][rev], 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.mincostflow(S, T, n//4+f)
print(ans)
0