結果

問題 No.2366 登校
ユーザー gew1fw
提出日時 2025-06-12 14:53:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,249 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,588 KB
実行使用メモリ 66,996 KB
最終ジャッジ日時 2025-06-12 14:56:27
合計ジャッジ時間 2,543 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr]); ptr +=1
    M = int(input[ptr]); ptr +=1
    K = int(input[ptr]); ptr +=1
    T = int(input[ptr]); ptr +=1

    S = N + M - 2
    if S <= T:
        print(0)
        return

    E = S - T
    if E <= 0:
        print(0)
        return

    squares = []
    for _ in range(K):
        A = int(input[ptr]); ptr +=1
        B = int(input[ptr]); ptr +=1
        C = int(input[ptr]); ptr +=1
        D = int(input[ptr]); ptr +=1
        w = C - 1
        c = D
        squares.append( (w, c) )

    a_min = float('inf')
    for w, c in squares:
        if w >= E:
            if c < a_min:
                a_min = c

    dp = [float('inf')] * (E + 1)
    dp[0] = 0

    for w, c in squares:
        for e in range(w, E + 1):
            if dp[e - w] + c < dp[e]:
                dp[e] = dp[e - w] + c

    possible = False
    min_cost = float('inf')

    if a_min != float('inf'):
        possible = True
        min_cost = a_min

    if dp[E] != float('inf'):
        possible = True
        if dp[E] < min_cost:
            min_cost = dp[E]

    if not possible:
        print(-1)
    else:
        print(min_cost)

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