結果

問題 No.1029 JJOOII 3
ユーザー tamatotamato
提出日時 2020-04-17 22:21:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 664 ms / 2,000 ms
コード長 3,002 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 83,956 KB
最終ジャッジ日時 2024-10-03 13:49:52
合計ジャッジ時間 9,771 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,540 KB
testcase_01 AC 33 ms
53,324 KB
testcase_02 AC 34 ms
52,932 KB
testcase_03 AC 109 ms
77,900 KB
testcase_04 AC 199 ms
81,424 KB
testcase_05 AC 312 ms
81,772 KB
testcase_06 AC 407 ms
80,948 KB
testcase_07 AC 316 ms
81,796 KB
testcase_08 AC 295 ms
81,728 KB
testcase_09 AC 273 ms
81,284 KB
testcase_10 AC 283 ms
81,716 KB
testcase_11 AC 322 ms
82,156 KB
testcase_12 AC 213 ms
79,760 KB
testcase_13 AC 379 ms
82,208 KB
testcase_14 AC 576 ms
83,600 KB
testcase_15 AC 36 ms
54,944 KB
testcase_16 AC 36 ms
54,452 KB
testcase_17 AC 37 ms
56,080 KB
testcase_18 AC 305 ms
82,840 KB
testcase_19 AC 40 ms
60,280 KB
testcase_20 AC 67 ms
74,924 KB
testcase_21 AC 67 ms
74,852 KB
testcase_22 AC 63 ms
74,596 KB
testcase_23 AC 66 ms
74,472 KB
testcase_24 AC 64 ms
73,712 KB
testcase_25 AC 61 ms
72,056 KB
testcase_26 AC 64 ms
73,680 KB
testcase_27 AC 72 ms
76,584 KB
testcase_28 AC 67 ms
74,788 KB
testcase_29 AC 64 ms
74,068 KB
testcase_30 AC 71 ms
76,368 KB
testcase_31 AC 66 ms
75,224 KB
testcase_32 AC 655 ms
83,788 KB
testcase_33 AC 652 ms
83,640 KB
testcase_34 AC 647 ms
83,600 KB
testcase_35 AC 664 ms
83,868 KB
testcase_36 AC 575 ms
83,956 KB
testcase_37 AC 33 ms
53,192 KB
testcase_38 AC 40 ms
62,020 KB
testcase_39 AC 32 ms
53,408 KB
testcase_40 AC 33 ms
52,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
inf = 10**20


def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    S_list = []
    C = []
    for _ in range(N):
        S, c = input().split()
        S_list.append(S)
        C.append(int(c))

    dp_J = [inf] * (K+1)
    dp_O = [inf] * (K+1)
    dp_I = [inf] * (K+1)
    dp_J[0] = 0
    J_list = []
    O_list = []
    I_list = []
    for S in S_list:
        J_list.append(S.count("J"))
        O_list.append(S.count("O"))
        I_list.append(S.count("I"))
    if sum(J_list) ==0 or sum(O_list) == 0 or sum(I_list) == 0:
        print(-1)
        exit()

    for i in range(K):
        for j in range(N):
            p = J_list[j]
            if not p:
                continue
            c = C[j]
            i_new = i+p
            if i_new < K:
                dp_J[i_new] = min(dp_J[i_new], dp_J[i] + c)
            else:
                cnt = K - i
                S = S_list[j]
                for k in range(len(S)):
                    if S[k] == "J":
                        cnt -= 1
                        if cnt == 0:
                            kk = k
                            break
                cnt = 0
                flg = 0
                for k in range(kk+1, len(S)):
                    if S[k] == "O":
                        cnt += 1
                        if cnt == K:
                            flg = 1
                            kkk = k
                            break
                if not flg:
                    dp_O[cnt] = min(dp_O[cnt], dp_J[i] + c)
                else:
                    cnt = 0
                    for k in range(kkk + 1, len(S)):
                        if S[k] == "I":
                            cnt += 1
                            if cnt == K:
                                break
                    dp_I[cnt] = min(dp_I[cnt], dp_J[i] + c)

    for i in range(K):
        for j in range(N):
            p = O_list[j]
            if not p:
                continue
            c = C[j]
            i_new = i+p
            if i_new < K:
                dp_O[i_new] = min(dp_O[i_new], dp_O[i] + c)
            else:
                cnt = K - i
                S = S_list[j]
                for k in range(len(S)):
                    if S[k] == "O":
                        cnt -= 1
                        if cnt == 0:
                            kk = k
                            break
                cnt = 0
                for k in range(kk+1, len(S)):
                    if S[k] == "I":
                        cnt += 1
                        if cnt == K:
                            break
                dp_I[cnt] = min(dp_I[cnt], dp_O[i] + c)

    for i in range(K):
        for j in range(N):
            p = I_list[j]
            if not p:
                continue
            c = C[j]
            i_new = min(i+p, K)
            dp_I[i_new] = min(dp_I[i_new], dp_I[i] + c)
    print(dp_I[-1])


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