結果

問題 No.1029 JJOOII 3
ユーザー 👑 tamatotamato
提出日時 2020-04-17 22:21:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 809 ms / 2,000 ms
コード長 3,002 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 83,960 KB
最終ジャッジ日時 2024-04-14 14:38:28
合計ジャッジ時間 11,417 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,324 KB
testcase_01 AC 39 ms
52,844 KB
testcase_02 AC 38 ms
54,492 KB
testcase_03 AC 125 ms
77,904 KB
testcase_04 AC 231 ms
81,820 KB
testcase_05 AC 343 ms
81,772 KB
testcase_06 AC 490 ms
81,220 KB
testcase_07 AC 363 ms
81,852 KB
testcase_08 AC 340 ms
81,884 KB
testcase_09 AC 315 ms
81,412 KB
testcase_10 AC 320 ms
81,660 KB
testcase_11 AC 372 ms
82,156 KB
testcase_12 AC 243 ms
79,484 KB
testcase_13 AC 425 ms
81,896 KB
testcase_14 AC 711 ms
83,520 KB
testcase_15 AC 42 ms
55,536 KB
testcase_16 AC 41 ms
55,600 KB
testcase_17 AC 42 ms
55,716 KB
testcase_18 AC 340 ms
82,844 KB
testcase_19 AC 46 ms
60,384 KB
testcase_20 AC 75 ms
75,236 KB
testcase_21 AC 76 ms
74,780 KB
testcase_22 AC 73 ms
74,672 KB
testcase_23 AC 75 ms
74,484 KB
testcase_24 AC 74 ms
74,016 KB
testcase_25 AC 68 ms
71,648 KB
testcase_26 AC 72 ms
73,352 KB
testcase_27 AC 83 ms
76,620 KB
testcase_28 AC 76 ms
74,460 KB
testcase_29 AC 74 ms
74,412 KB
testcase_30 AC 82 ms
76,360 KB
testcase_31 AC 76 ms
75,120 KB
testcase_32 AC 809 ms
83,836 KB
testcase_33 AC 800 ms
83,612 KB
testcase_34 AC 796 ms
83,580 KB
testcase_35 AC 806 ms
83,960 KB
testcase_36 AC 716 ms
83,724 KB
testcase_37 AC 38 ms
54,168 KB
testcase_38 AC 47 ms
61,244 KB
testcase_39 AC 38 ms
53,052 KB
testcase_40 AC 38 ms
53,208 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