結果

問題 No.1288 yuki collection
ユーザー chocoruskchocorusk
提出日時 2020-09-25 03:33:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 934 ms / 5,000 ms
コード長 2,944 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 85,812 KB
最終ジャッジ日時 2023-09-10 14:37:37
合計ジャッジ時間 24,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,340 KB
testcase_01 AC 82 ms
71,324 KB
testcase_02 AC 82 ms
71,380 KB
testcase_03 AC 122 ms
77,304 KB
testcase_04 AC 81 ms
71,400 KB
testcase_05 AC 81 ms
71,244 KB
testcase_06 AC 86 ms
75,352 KB
testcase_07 AC 79 ms
71,324 KB
testcase_08 AC 133 ms
77,516 KB
testcase_09 AC 125 ms
77,444 KB
testcase_10 AC 129 ms
77,424 KB
testcase_11 AC 112 ms
77,164 KB
testcase_12 AC 136 ms
78,080 KB
testcase_13 AC 896 ms
85,132 KB
testcase_14 AC 860 ms
84,788 KB
testcase_15 AC 745 ms
83,592 KB
testcase_16 AC 762 ms
84,344 KB
testcase_17 AC 911 ms
85,268 KB
testcase_18 AC 921 ms
85,812 KB
testcase_19 AC 910 ms
85,236 KB
testcase_20 AC 817 ms
81,544 KB
testcase_21 AC 902 ms
82,520 KB
testcase_22 AC 931 ms
85,128 KB
testcase_23 AC 925 ms
85,736 KB
testcase_24 AC 922 ms
84,904 KB
testcase_25 AC 840 ms
82,368 KB
testcase_26 AC 934 ms
85,044 KB
testcase_27 AC 547 ms
82,588 KB
testcase_28 AC 589 ms
83,048 KB
testcase_29 AC 506 ms
82,832 KB
testcase_30 AC 195 ms
80,552 KB
testcase_31 AC 210 ms
80,232 KB
testcase_32 AC 213 ms
80,140 KB
testcase_33 AC 857 ms
81,996 KB
testcase_34 AC 890 ms
84,948 KB
testcase_35 AC 876 ms
84,996 KB
testcase_36 AC 909 ms
82,192 KB
testcase_37 AC 925 ms
82,900 KB
testcase_38 AC 492 ms
80,324 KB
testcase_39 AC 500 ms
80,412 KB
testcase_40 AC 146 ms
78,852 KB
testcase_41 AC 81 ms
71,592 KB
testcase_42 AC 82 ms
71,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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()))
inf=10**9
mcf=MinCostFlow(n+1)
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(i, idx[k+1], 1, inf-v[i])
    idx[k]=i
if idx[0]==n+1:
    print(0)
    exit()
mcf.add_edge(idx[0], n, n//4, inf*4)
print(-mcf.flow(idx[0], n, n//4)+inf*4*(n//4))
0