結果

問題 No.1288 yuki collection
ユーザー hiragnhiragn
提出日時 2022-12-13 16:21:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,716 ms / 5,000 ms
コード長 2,728 bytes
コンパイル時間 107 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-04-25 01:50:43
合計ジャッジ時間 52,581 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,136 KB
testcase_01 AC 29 ms
11,008 KB
testcase_02 AC 27 ms
11,136 KB
testcase_03 AC 29 ms
11,008 KB
testcase_04 AC 27 ms
11,008 KB
testcase_05 AC 28 ms
10,880 KB
testcase_06 AC 28 ms
10,880 KB
testcase_07 AC 27 ms
11,136 KB
testcase_08 AC 30 ms
11,136 KB
testcase_09 AC 28 ms
10,880 KB
testcase_10 AC 32 ms
11,136 KB
testcase_11 AC 28 ms
10,880 KB
testcase_12 AC 30 ms
11,008 KB
testcase_13 AC 2,595 ms
12,672 KB
testcase_14 AC 2,472 ms
12,672 KB
testcase_15 AC 2,016 ms
12,416 KB
testcase_16 AC 2,027 ms
12,672 KB
testcase_17 AC 2,716 ms
12,544 KB
testcase_18 AC 2,559 ms
12,800 KB
testcase_19 AC 2,646 ms
12,800 KB
testcase_20 AC 2,525 ms
12,800 KB
testcase_21 AC 2,131 ms
12,672 KB
testcase_22 AC 2,125 ms
12,544 KB
testcase_23 AC 2,124 ms
12,672 KB
testcase_24 AC 2,707 ms
12,800 KB
testcase_25 AC 2,624 ms
12,800 KB
testcase_26 AC 2,528 ms
12,800 KB
testcase_27 AC 1,043 ms
12,544 KB
testcase_28 AC 1,279 ms
12,672 KB
testcase_29 AC 1,551 ms
12,544 KB
testcase_30 AC 108 ms
12,544 KB
testcase_31 AC 142 ms
12,544 KB
testcase_32 AC 147 ms
12,544 KB
testcase_33 AC 2,229 ms
12,672 KB
testcase_34 AC 1,809 ms
12,544 KB
testcase_35 AC 1,703 ms
12,672 KB
testcase_36 AC 1,975 ms
12,800 KB
testcase_37 AC 1,990 ms
12,672 KB
testcase_38 AC 1,151 ms
12,672 KB
testcase_39 AC 1,156 ms
12,672 KB
testcase_40 AC 61 ms
12,416 KB
testcase_41 AC 29 ms
10,880 KB
testcase_42 AC 29 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop


class MinCostFlow:
    INF = 10 ** 18

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

    def add_edge(self, fr, to, cap, cost):
        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

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

        res = 0
        H = [0] * N
        prv_v = [0] * N
        prv_e = [0] * N

        d0 = [INF] * N
        dist = [INF] * N

        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                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]
                        prv_v[w] = v
                        prv_e[w] = e
                        heappush(que, (r, w))
            if dist[t] == INF:
                return None

            for i in range(N):
                H[i] += dist[i]

            d = f
            v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res


def main():
    n = int(input())
    s = input()
    v = [0] + list(map(int, input().split()))

    d = {"y": 0, "u": 1, "k": 2, "i": 3}
    idx = [[] for _ in range(4)]
    for i, c in enumerate(s, 1):
        idx[d[c]].append(i)

    g = MinCostFlow(n + 2)
    infty = 10 ** 10
    # 始点から1個目のyへ
    if idx[0]:
        g.add_edge(0, idx[0][0], infty, 0)

    # すべてのiから終点へ
    for x in idx[-1]:
        g.add_edge(x, n + 1, 1, infty - v[x])

    # y-u, u-k, k-i間
    for i in range(3):
        for fr in idx[i]:
            to = next(filter(lambda t: t > fr, idx[i + 1]), None)
            if to:
                g.add_edge(fr, to, 1, infty - v[fr])

    # 同じ文字間
    for x in idx:
        for i in range(len(x) - 1):
            g.add_edge(x[i], x[i + 1], infty, 0)

    # 結果表示
    ans = 0
    while True:
        tmp = g.flow(0, n + 1, 1)
        if tmp is None:
            break
        tmp = 4 * infty - tmp
        if tmp < 0:
            break
        ans += tmp
    print(ans)


if __name__ == "__main__":
    main()
    
0