結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,388 KB
testcase_01 AC 41 ms
56,940 KB
testcase_02 AC 40 ms
53,388 KB
testcase_03 AC 41 ms
56,940 KB
testcase_04 AC 39 ms
53,388 KB
testcase_05 AC 53 ms
65,484 KB
testcase_06 AC 85 ms
74,824 KB
testcase_07 AC 74 ms
74,732 KB
testcase_08 AC 51 ms
63,280 KB
testcase_09 AC 38 ms
53,388 KB
testcase_10 AC 82 ms
74,796 KB
testcase_11 AC 60 ms
71,672 KB
testcase_12 AC 53 ms
65,492 KB
testcase_13 AC 85 ms
74,840 KB
testcase_14 AC 50 ms
63,432 KB
testcase_15 AC 59 ms
69,640 KB
testcase_16 AC 43 ms
56,940 KB
testcase_17 AC 54 ms
65,484 KB
testcase_18 AC 57 ms
67,560 KB
testcase_19 AC 43 ms
56,940 KB
testcase_20 AC 50 ms
63,272 KB
testcase_21 AC 88 ms
74,820 KB
testcase_22 AC 70 ms
74,748 KB
testcase_23 AC 83 ms
74,772 KB
testcase_24 AC 46 ms
59,176 KB
testcase_25 AC 61 ms
72,112 KB
testcase_26 AC 59 ms
71,692 KB
testcase_27 AC 60 ms
71,704 KB
testcase_28 AC 59 ms
69,644 KB
testcase_29 AC 90 ms
74,836 KB
testcase_30 AC 39 ms
53,388 KB
testcase_31 AC 47 ms
59,188 KB
testcase_32 AC 48 ms
61,236 KB
testcase_33 AC 47 ms
59,188 KB
testcase_34 AC 47 ms
61,236 KB
testcase_35 AC 59 ms
69,652 KB
testcase_36 AC 41 ms
56,940 KB
testcase_37 AC 55 ms
65,516 KB
testcase_38 AC 57 ms
67,572 KB
testcase_39 AC 55 ms
67,556 KB
権限があれば一括ダウンロードができます

ソースコード

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[j] > V:
            maxw = j - 1
            break
    print(minw)
    print(maxw)

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