結果

問題 No.1029 JJOOII 3
ユーザー mkawa2mkawa2
提出日時 2021-04-16 17:55:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,007 ms / 2,000 ms
コード長 2,492 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 86,964 KB
実行使用メモリ 83,044 KB
最終ジャッジ日時 2023-09-15 15:33:57
合計ジャッジ時間 17,498 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,268 KB
testcase_01 AC 72 ms
71,292 KB
testcase_02 AC 73 ms
71,524 KB
testcase_03 AC 161 ms
78,072 KB
testcase_04 AC 157 ms
79,788 KB
testcase_05 AC 473 ms
81,196 KB
testcase_06 AC 439 ms
79,888 KB
testcase_07 AC 398 ms
80,912 KB
testcase_08 AC 449 ms
81,436 KB
testcase_09 AC 363 ms
80,992 KB
testcase_10 AC 382 ms
81,100 KB
testcase_11 AC 511 ms
81,880 KB
testcase_12 AC 383 ms
79,860 KB
testcase_13 AC 389 ms
81,420 KB
testcase_14 AC 481 ms
81,540 KB
testcase_15 AC 308 ms
81,988 KB
testcase_16 AC 293 ms
82,460 KB
testcase_17 AC 481 ms
82,184 KB
testcase_18 AC 406 ms
83,044 KB
testcase_19 AC 104 ms
77,112 KB
testcase_20 AC 304 ms
77,640 KB
testcase_21 AC 329 ms
78,748 KB
testcase_22 AC 355 ms
78,184 KB
testcase_23 AC 289 ms
78,288 KB
testcase_24 AC 317 ms
78,172 KB
testcase_25 AC 318 ms
77,940 KB
testcase_26 AC 364 ms
77,488 KB
testcase_27 AC 448 ms
77,952 KB
testcase_28 AC 351 ms
78,632 KB
testcase_29 AC 352 ms
78,444 KB
testcase_30 AC 384 ms
77,808 KB
testcase_31 AC 333 ms
77,820 KB
testcase_32 AC 1,002 ms
82,000 KB
testcase_33 AC 1,007 ms
82,008 KB
testcase_34 AC 846 ms
81,984 KB
testcase_35 AC 953 ms
81,688 KB
testcase_36 AC 877 ms
81,848 KB
testcase_37 AC 70 ms
71,292 KB
testcase_38 AC 100 ms
77,080 KB
testcase_39 AC 70 ms
71,332 KB
testcase_40 AC 71 ms
71,216 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