結果

問題 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,927 ms / 5,000 ms
コード長 2,728 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-11-07 11:18:22
合計ジャッジ時間 56,649 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,136 KB
testcase_01 AC 30 ms
11,136 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 34 ms
11,008 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 33 ms
11,008 KB
testcase_06 AC 32 ms
11,008 KB
testcase_07 AC 31 ms
11,136 KB
testcase_08 AC 35 ms
11,264 KB
testcase_09 AC 34 ms
11,136 KB
testcase_10 AC 35 ms
11,136 KB
testcase_11 AC 32 ms
11,136 KB
testcase_12 AC 35 ms
11,136 KB
testcase_13 AC 2,772 ms
12,672 KB
testcase_14 AC 2,596 ms
12,672 KB
testcase_15 AC 2,150 ms
12,544 KB
testcase_16 AC 2,144 ms
12,544 KB
testcase_17 AC 2,621 ms
12,672 KB
testcase_18 AC 2,894 ms
12,800 KB
testcase_19 AC 2,895 ms
12,672 KB
testcase_20 AC 2,726 ms
12,800 KB
testcase_21 AC 2,253 ms
12,672 KB
testcase_22 AC 2,260 ms
12,672 KB
testcase_23 AC 2,250 ms
12,800 KB
testcase_24 AC 2,927 ms
12,672 KB
testcase_25 AC 2,864 ms
12,800 KB
testcase_26 AC 2,769 ms
12,672 KB
testcase_27 AC 1,137 ms
12,672 KB
testcase_28 AC 1,430 ms
12,672 KB
testcase_29 AC 1,737 ms
12,672 KB
testcase_30 AC 120 ms
12,544 KB
testcase_31 AC 157 ms
12,672 KB
testcase_32 AC 160 ms
12,544 KB
testcase_33 AC 2,354 ms
12,672 KB
testcase_34 AC 1,939 ms
12,544 KB
testcase_35 AC 1,847 ms
12,800 KB
testcase_36 AC 2,151 ms
12,672 KB
testcase_37 AC 2,137 ms
12,672 KB
testcase_38 AC 1,259 ms
12,544 KB
testcase_39 AC 1,267 ms
12,544 KB
testcase_40 AC 67 ms
12,416 KB
testcase_41 AC 31 ms
11,008 KB
testcase_42 AC 31 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