結果

問題 No.1029 JJOOII 3
ユーザー mkawa2mkawa2
提出日時 2021-04-16 17:55:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,033 ms / 2,000 ms
コード長 2,492 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 84,480 KB
最終ジャッジ日時 2024-07-02 18:14:27
合計ジャッジ時間 16,844 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 42 ms
52,992 KB
testcase_03 AC 142 ms
76,380 KB
testcase_04 AC 147 ms
79,104 KB
testcase_05 AC 485 ms
80,128 KB
testcase_06 AC 439 ms
78,720 KB
testcase_07 AC 393 ms
79,488 KB
testcase_08 AC 438 ms
80,128 KB
testcase_09 AC 356 ms
79,360 KB
testcase_10 AC 375 ms
79,616 KB
testcase_11 AC 501 ms
80,512 KB
testcase_12 AC 381 ms
78,268 KB
testcase_13 AC 388 ms
80,384 KB
testcase_14 AC 475 ms
80,640 KB
testcase_15 AC 304 ms
81,408 KB
testcase_16 AC 286 ms
81,792 KB
testcase_17 AC 482 ms
84,480 KB
testcase_18 AC 389 ms
81,476 KB
testcase_19 AC 81 ms
72,704 KB
testcase_20 AC 294 ms
76,796 KB
testcase_21 AC 314 ms
77,104 KB
testcase_22 AC 342 ms
76,424 KB
testcase_23 AC 273 ms
76,168 KB
testcase_24 AC 300 ms
76,212 KB
testcase_25 AC 311 ms
76,720 KB
testcase_26 AC 360 ms
76,676 KB
testcase_27 AC 455 ms
76,552 KB
testcase_28 AC 349 ms
76,552 KB
testcase_29 AC 347 ms
76,712 KB
testcase_30 AC 387 ms
76,816 KB
testcase_31 AC 326 ms
76,288 KB
testcase_32 AC 1,017 ms
80,384 KB
testcase_33 AC 1,033 ms
80,256 KB
testcase_34 AC 873 ms
80,512 KB
testcase_35 AC 978 ms
80,384 KB
testcase_36 AC 917 ms
80,640 KB
testcase_37 AC 42 ms
52,736 KB
testcase_38 AC 80 ms
71,936 KB
testcase_39 AC 42 ms
52,480 KB
testcase_40 AC 44 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def FI(): return float(sys.stdin.buffer.readline())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
dij = [(0, 1), (1, 0), (1, 1), (1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

n, k = LI()
ss = []
cc = []
for _ in range(n):
    s, c = SI().split()
    ss.append(s)
    cc.append(int(c))

dpj = [0]+[inf]*k
dpo = [0]+[inf]*k
dpi = [0]+[inf]*k

def update(dp, cnt):
    for i in range(k+1):
        pi = max(0, i-cnt)
        dp[i] = min(dp[i], dp[pi]+cost)

for s, cost in zip(ss, cc):
    cj = co = ci = 0
    for c in s:
        if c == "J": cj += 1
        elif c == "O": co += 1
        elif c == "I": ci += 1
    update(dpj, cj)
    update(dpo, co)
    update(dpi, ci)

joi = [[s.count("J"), s.count("O"), s.count("I")]*3 for s in ss]
ans = dpj[k]+dpo[k]+dpi[k]
for i, (s, cost) in enumerate(zip(ss, cc)):
    m = len(s)
    cj = co = 0
    ci = joi[i][2]
    l = r = 0
    while r < m:
        while r < m and co < k:
            if s[r] == "O": co += 1
            elif s[r] == "I": ci -= 1
            r += 1
        if co < k: break
        while l < r and co == k:
            if s[l] == "J": cj += 1
            elif s[l] == "O": co -= 1
            l += 1
        cur = dpj[max(0, k-cj)]+cost+dpi[max(0, k-ci)]
        if cur < ans: ans = cur
        # print(s, l, r, cj, co, ci, ans)

for i, (s0, cost0) in enumerate(zip(ss, cc)):
    for j, (s1, cost1) in enumerate(zip(ss, cc)):

        cj, co0 = 0, joi[i][1]
        for c0 in s0:
            if c0 == "I": continue
            if c0 == "J": cj += 1
            if c0 == "O": co0 -= 1

            co1, ci = 0, joi[j][2]
            for c1 in s1:
                if c1 == "J": continue
                if c1 == "O": co1 += 1
                if c1 == "I": ci -= 1

                ans = min(ans, dpj[max(0, k-cj)]+dpo[max(0, k-co0-co1)]+dpi[max(0, k-ci)]+cost0+cost1)

if ans >= inf: ans = -1
print(ans)
0