結果

問題 No.1288 yuki collection
ユーザー chocoruskchocorusk
提出日時 2020-08-22 11:42:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,403 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 155,072 KB
最終ジャッジ日時 2023-09-10 13:34:49
合計ジャッジ時間 29,536 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
75,732 KB
testcase_01 AC 76 ms
71,384 KB
testcase_02 AC 91 ms
76,476 KB
testcase_03 AC 170 ms
78,560 KB
testcase_04 AC 77 ms
71,428 KB
testcase_05 AC 85 ms
75,628 KB
testcase_06 AC 161 ms
78,740 KB
testcase_07 AC 89 ms
76,368 KB
testcase_08 AC 176 ms
78,848 KB
testcase_09 AC 162 ms
78,372 KB
testcase_10 AC 189 ms
78,900 KB
testcase_11 AC 163 ms
78,216 KB
testcase_12 AC 170 ms
78,848 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 4,389 ms
129,516 KB
testcase_16 AC 4,239 ms
127,388 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class MinCostFlow:
    INF=10**18
    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):
        self.g[frm].append((to, cap, cost, len(self.g[to])))
        self.g[to].append((frm, 0, -cost, len(self.g[frm])-1))
    def mincostflow(self, s, t, f):
        res=0
        self.h=[0]*self.n
        while f>0:
            que=[]
            dist=[self.INF]*self.n
            dist[s]=0
            heapq.heappush(que, (0, s))
            prevv=[0]*self.n
            preve=[0]*self.n
            while len(que)>0:
                p=heapq.heappop(que)
                if dist[p[1]]<p[0]:
                    continue
                for i in range(len(self.g[p[1]])):
                    e=self.g[p[1]][i]
                    if e[1]>0 and dist[e[0]]>dist[p[1]]+e[2]+self.h[p[1]]-self.h[e[0]]:
                        dist[e[0]]=dist[p[1]]+e[2]+self.h[p[1]]-self.h[e[0]]
                        prevv[e[0]]=p[1]
                        preve[e[0]]=i
                        heapq.heappush(que, (dist[e[0]], e[0]))
            self.h=[a+b for a, b in zip(self.h, dist)]
            if dist[t]==self.INF:
                return -1
            d=f
            v=t
            while v!=s:
                d=min(d, self.g[prevv[v]][preve[v]][1])
                v=prevv[v]
            f-=d
            res+=d*self.h[t]
            v=t
            def add(e, d):
                return (e[0], e[1]+d, e[2], e[3])
            while v!=s:
                self.g[prevv[v]][preve[v]]=add(self.g[prevv[v]][preve[v]], -d)
                self.g[v][self.g[prevv[v]][preve[v]][3]]=add(self.g[v][self.g[prevv[v]][preve[v]][3]], 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