結果

問題 No.1288 yuki collection
ユーザー toyuzukotoyuzuko
提出日時 2020-12-06 18:41:04
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,822 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 87,820 KB
最終ジャッジ日時 2024-09-17 13:09:50
合計ジャッジ時間 19,584 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,248 KB
testcase_01 AC 42 ms
53,376 KB
testcase_02 AC 41 ms
53,376 KB
testcase_03 AC 69 ms
71,040 KB
testcase_04 RE -
testcase_05 AC 41 ms
53,120 KB
testcase_06 AC 43 ms
54,272 KB
testcase_07 AC 42 ms
53,248 KB
testcase_08 AC 96 ms
76,060 KB
testcase_09 AC 80 ms
75,008 KB
testcase_10 AC 104 ms
76,120 KB
testcase_11 AC 64 ms
68,292 KB
testcase_12 AC 95 ms
76,288 KB
testcase_13 AC 716 ms
85,104 KB
testcase_14 AC 728 ms
84,264 KB
testcase_15 AC 612 ms
83,720 KB
testcase_16 AC 621 ms
83,468 KB
testcase_17 AC 713 ms
85,036 KB
testcase_18 WA -
testcase_19 AC 722 ms
84,792 KB
testcase_20 AC 735 ms
84,992 KB
testcase_21 AC 845 ms
86,472 KB
testcase_22 AC 866 ms
86,624 KB
testcase_23 AC 840 ms
86,164 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 484 ms
81,568 KB
testcase_28 AC 504 ms
82,144 KB
testcase_29 WA -
testcase_30 AC 159 ms
78,628 KB
testcase_31 AC 178 ms
78,896 KB
testcase_32 AC 184 ms
79,208 KB
testcase_33 AC 768 ms
87,820 KB
testcase_34 AC 943 ms
87,664 KB
testcase_35 AC 889 ms
86,888 KB
testcase_36 AC 594 ms
83,936 KB
testcase_37 AC 666 ms
85,160 KB
testcase_38 AC 377 ms
80,116 KB
testcase_39 AC 404 ms
80,924 KB
testcase_40 AC 117 ms
78,196 KB
testcase_41 AC 40 ms
53,632 KB
testcase_42 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush, heapify

class MinCostFlow():
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

    def add_edge(self, fr, to, cap, cost):
        #assert 0 <= fr < self.n
        #assert 0 <= to < self.n
        m = len(self.pos)
        self.pos.append((fr, len(self.graph[fr])))
        self.graph[fr].append([to, len(self.graph[to]), cap, cost])
        self.graph[to].append([fr, len(self.graph[fr]) - 1, 0, -cost])
        return m

    def get_edge(self, idx):
        #assert 0 <= idx < len(self.pos)
        to, rev, cap, cost = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap, rev_cost = self.graph[to][rev]
        return self.pos[idx][0], to, cap + rev_cap, rev_cap, cost

    def edges(self):
        for i in range(len(self.pos)):
            yield self.get_edge(i)

    def dual_ref(self, s, t):
        dist = [2**63 - 1] * self.n
        dist[s] = 0
        vis = [0] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        queue = []
        heappush(queue, (0, s))
        while queue:
            k, v = heappop(queue)
            if vis[v]: continue
            vis[v] = True
            if v == t: break
            for i in range(len(self.graph[v])):
                to, rev, cap, cost = self.graph[v][i]
                if vis[to] or cap == 0: continue
                cost += self.dual[v] - self.dual[to]
                if dist[to] - dist[v] > cost:
                    dist[to] = dist[v] + cost
                    self.pv[to] = v
                    self.pe[to] = i
                    heappush(queue, (dist[to], to))
        if not vis[t]: return False
        for v in range(self.n):
            if not vis[v]: continue
            self.dual[v] -= dist[t] - dist[v]
        return True

    def flow(self, s, t):
        return self.flow_with_limit(s, t, 2**63 - 1)

    def flow_with_limit(self, s, t, limit):
        return self.slope_with_limit(s, t, limit)[-1]

    def slope(self, s, t):
        return self.slope_with_limit(s, t, 2**63 - 1)

    def slope_with_limit(self, s, t, limit):
        #assert 0 <= s < self.n
        #assert 0 <= t < self.n
        #assert s != t
        flow = 0
        cost = 0
        prev_cost = -1
        res = [(flow, cost)]
        self.dual = [0] * self.n
        while flow < limit:
            if not self.dual_ref(s, t): break
            c = limit - flow
            v = t
            while v != s:
                c = min(c, self.graph[self.pv[v]][self.pe[v]][2])
                v = self.pv[v]
            v = t
            while v != s:
                to, rev, cap, _ = self.graph[self.pv[v]][self.pe[v]]
                self.graph[self.pv[v]][self.pe[v]][2] -= c
                self.graph[v][rev][2] += c
                v = self.pv[v]
            d = -self.dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                res.pop()
            res.append((flow, cost))
            prev_cost = cost
        return res

N = int(input())
S = input()

V = list(map(int, input().split()))

INF = 10**18
MAX = 10**15

mcf = MinCostFlow(N + 2)

s = N
t = N + 1

arr = [0] * N

for i in range(N):
    if S[i] == 'y':
        pass
    elif S[i] == 'u':
        arr[i] = 1
    elif S[i] == 'k':
        arr[i] = 2
    else:
        arr[i] = 3

rm = [None] * 4

for i in range(N)[::-1]:
    if arr[i] == 3:
        mcf.add_edge(i, t, 1, MAX - V[i])
    if rm[arr[i]] is not None:
        mcf.add_edge(i, rm[arr[i]], INF, 0)
    if arr[i] != 3 and rm[arr[i] + 1] is not None:
        mcf.add_edge(i, rm[arr[i] + 1], 1, MAX - V[i])
    rm[arr[i]] = i

if rm[arr[0]] is not None:
    mcf.add_edge(s, rm[0], INF, 0)

flow, cost = mcf.flow(s, t)

print(MAX * flow * 4 - cost)
0