結果

問題 No.1288 yuki collection
ユーザー ああいいああいい
提出日時 2022-04-13 22:14:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,690 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 79,172 KB
最終ジャッジ日時 2023-08-25 14:03:38
合計ジャッジ時間 9,475 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,556 KB
testcase_01 AC 76 ms
71,460 KB
testcase_02 AC 76 ms
71,112 KB
testcase_03 AC 142 ms
78,128 KB
testcase_04 AC 76 ms
71,660 KB
testcase_05 AC 76 ms
71,436 KB
testcase_06 AC 94 ms
76,684 KB
testcase_07 AC 75 ms
71,316 KB
testcase_08 AC 151 ms
78,728 KB
testcase_09 AC 139 ms
77,956 KB
testcase_10 AC 167 ms
79,172 KB
testcase_11 AC 125 ms
77,660 KB
testcase_12 AC 150 ms
78,804 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
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 #

#その2 最初はコストが全部正の場合に使えるダイクストラ
from heapq import heappush,heappop
class MinCostFlow:
    inf = 10 ** 18

    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.H = [0] * N
        self.edge = []

    def add_edge(self,fr,to,cap,cost):
        e = [to,cap,cost,None]
        r = e[3] = [fr,0,-cost,e]
        self.G[fr].append(e)
        self.G[to].append(r)
        self.edge.append(e)
    def get_edge(self,i):
        return self.edge[i]

    def flow(self,s,t,f):
        N = self.N
        G = self.G
        inf = MinCostFlow.inf

        res = 0
        H = self.H  #ポテンシャル、コストが最初は正なので、最初は0でいい
        prev_v = [0] * N
        prev_e = [None] * N

        d0 = [inf] * N
        dist = [inf] * N
        while f:
            dist[:] = d0
            dist[s] = 0
            q = [(0,s)]
            while q:
                c,v = heappop(q)
                if dist[v] < c:continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w,cap,cost,_ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prev_v[w] = v
                        prev_e[w] = e
                        heappush(q,(r,w))
            if dist[t] == inf:
                return None
            """
            for i in range(N):
                H[i] += dist[i]
            """
            H = [h + d for h,d in zip(H,dist)]
            d = f
            v = t
            while v != s:
                d = min(d,prev_e[v][1])
                v = prev_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prev_e[v]
                e[1] -= d
                e[3][1] += d
                v = prev_v[v]
        return res

N = int(input())
S = input()
V = list(map(int,input().split()))
d = {"y":0,"u":1,"k":2,"i":3}
dat = [[] for _ in range(4)]
for i in range(N):
    dat[d[S[i]]].append(i)
s = 0
t = N + 1
inf = 10 ** 10
mincost = MinCostFlow(N+2 + N)
base = N + 2
for i in range(3):
    a = dat[i]
    b = dat[i+1]
    for x in a:
        for y in b:
            if y < x:continue
            mincost.add_edge(x+base,y+1,1,0)
            #print(x+1,y+1)
for x in dat[-1]:
    mincost.add_edge(x+base,t,1,0)
for x in dat[0]:
    mincost.add_edge(s,x+1,1,0)
for x in range(N):
    mincost.add_edge(x+1,x + base,1,inf - V[x])

ans = 0
while True:
    tmp = mincost.flow(s,t,1)
    if tmp == None:break
    tmp = inf * 4 - tmp
    #print(tmp)
    ans += tmp
print(ans)
#print(dat)
0