結果

問題 No.527 ナップサック容量問題
ユーザー rpy3cpprpy3cpp
提出日時 2017-06-09 22:49:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 798 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 81,560 KB
実行使用メモリ 74,840 KB
最終ジャッジ日時 2023-10-23 22:21:13
合計ジャッジ時間 4,919 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 34 ms
53,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 47 ms
65,484 KB
testcase_06 AC 78 ms
74,828 KB
testcase_07 WA -
testcase_08 AC 45 ms
63,280 KB
testcase_09 AC 34 ms
53,380 KB
testcase_10 WA -
testcase_11 AC 56 ms
71,672 KB
testcase_12 AC 49 ms
65,492 KB
testcase_13 AC 80 ms
74,840 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 35 ms
53,380 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(input())
    vs = []
    ws = []
    for _ in range(N):
        v, w = map(int, input().split())
        vs.append(v)
        ws.append(w)
    V = int(input())
    return N, vs, ws, V

def solve(N, vs, ws, V):
    W = sum(ws)
    dp = [0] * (W + 1)
    for v, w in zip(vs, ws):
        for imw in range(W - w, 0, -1):
            if dp[imw]:
                newdp = dp[imw] + v
                if newdp > dp[imw + w]: dp[imw + w] = newdp
        dp[w] = max(dp[w], v)
    minw = 1
    maxw = 'inf'
    for i in range(1, W + 1):
        if dp[i] == V:
            minw = i
            break
    for j in range(i + 1, W + 1):
        if dp[i] > V:
            maxw = j - 1
            break
    print(minw)
    print(maxw)

N, vs, ws, V = read_data()
solve(N, vs, ws, V)
0