結果

問題 No.527 ナップサック容量問題
ユーザー lllllll88938494lllllll88938494
提出日時 2022-09-15 13:22:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 872 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 66,816 KB
最終ジャッジ日時 2024-05-09 15:06:47
合計ジャッジ時間 4,659 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,096 KB
testcase_01 AC 38 ms
57,856 KB
testcase_02 AC 37 ms
52,480 KB
testcase_03 AC 39 ms
58,112 KB
testcase_04 AC 35 ms
52,352 KB
testcase_05 AC 67 ms
64,768 KB
testcase_06 AC 130 ms
65,600 KB
testcase_07 AC 102 ms
64,896 KB
testcase_08 AC 63 ms
64,512 KB
testcase_09 AC 36 ms
51,840 KB
testcase_10 AC 136 ms
66,304 KB
testcase_11 AC 91 ms
65,792 KB
testcase_12 AC 72 ms
64,640 KB
testcase_13 AC 139 ms
65,664 KB
testcase_14 AC 60 ms
63,488 KB
testcase_15 AC 80 ms
64,768 KB
testcase_16 AC 40 ms
58,112 KB
testcase_17 AC 74 ms
65,920 KB
testcase_18 AC 73 ms
64,640 KB
testcase_19 AC 39 ms
58,112 KB
testcase_20 AC 67 ms
66,048 KB
testcase_21 AC 158 ms
65,920 KB
testcase_22 AC 102 ms
64,768 KB
testcase_23 AC 132 ms
65,408 KB
testcase_24 AC 55 ms
63,872 KB
testcase_25 AC 89 ms
64,512 KB
testcase_26 AC 87 ms
64,384 KB
testcase_27 AC 87 ms
64,128 KB
testcase_28 AC 86 ms
65,280 KB
testcase_29 AC 156 ms
64,868 KB
testcase_30 AC 38 ms
52,224 KB
testcase_31 AC 53 ms
63,616 KB
testcase_32 AC 60 ms
66,816 KB
testcase_33 AC 53 ms
64,384 KB
testcase_34 AC 55 ms
65,792 KB
testcase_35 AC 86 ms
64,640 KB
testcase_36 AC 37 ms
57,856 KB
testcase_37 AC 71 ms
64,512 KB
testcase_38 AC 79 ms
65,792 KB
testcase_39 AC 78 ms
64,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
m=[list(map(int,input().split())) for i in range(n)]
maxw=0
for i in range(n):
    maxw += m[i][1]
v = int(input())
dp=[[-1]*(maxw+1) for i in range(2)]
dp[0][0] = 0

for i in range(n):
    for j in range(maxw+1):
        if dp[i%2][j] == -1:
            continue
        dp[(i+1)%2][j] = max(dp[(i+1)%2][j],dp[i%2][j])
        if j + m[i][1] <= len(dp[0])-1:
            dp[(i+1)%2][j+m[i][1]] = max(dp[(i+1)%2][j+m[i][1]],dp[i%2][j] + m[i][0])

ans=[]
for i in range(maxw+1):
    if dp[n%2][i] == v:
        while dp[n%2][i] <= v:
            ans.append(i)
            i+=1
            if i >= maxw+1:
                break
        else:
            break
        
# print(dp[n%2])
# print(ans)
if dp[n%2][-1] == v:
    print(maxw)
    print('inf')
else:
    print(1 if ans[0] == 0 else ans[0]  )
    print(ans[-1])
                
            
    
    
0