結果

問題 No.527 ナップサック容量問題
ユーザー lllllll88938494lllllll88938494
提出日時 2022-09-15 13:22:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 872 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 77,024 KB
最終ジャッジ日時 2023-08-22 09:30:44
合計ジャッジ時間 7,011 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,464 KB
testcase_01 AC 75 ms
76,008 KB
testcase_02 AC 74 ms
71,320 KB
testcase_03 AC 75 ms
75,796 KB
testcase_04 AC 75 ms
71,624 KB
testcase_05 AC 110 ms
76,604 KB
testcase_06 AC 173 ms
76,900 KB
testcase_07 AC 149 ms
76,772 KB
testcase_08 AC 102 ms
76,228 KB
testcase_09 AC 74 ms
71,496 KB
testcase_10 AC 181 ms
76,720 KB
testcase_11 AC 129 ms
76,472 KB
testcase_12 AC 110 ms
76,760 KB
testcase_13 AC 176 ms
76,744 KB
testcase_14 AC 99 ms
76,664 KB
testcase_15 AC 121 ms
76,404 KB
testcase_16 AC 80 ms
75,716 KB
testcase_17 AC 114 ms
76,396 KB
testcase_18 AC 113 ms
76,748 KB
testcase_19 AC 80 ms
75,936 KB
testcase_20 AC 102 ms
76,256 KB
testcase_21 AC 207 ms
77,024 KB
testcase_22 AC 144 ms
76,748 KB
testcase_23 AC 174 ms
76,924 KB
testcase_24 AC 89 ms
76,036 KB
testcase_25 AC 130 ms
76,468 KB
testcase_26 AC 128 ms
76,688 KB
testcase_27 AC 129 ms
76,640 KB
testcase_28 AC 124 ms
76,472 KB
testcase_29 AC 196 ms
76,892 KB
testcase_30 AC 74 ms
71,368 KB
testcase_31 AC 90 ms
76,128 KB
testcase_32 AC 95 ms
76,952 KB
testcase_33 AC 90 ms
76,208 KB
testcase_34 AC 95 ms
76,252 KB
testcase_35 AC 130 ms
76,592 KB
testcase_36 AC 78 ms
75,720 KB
testcase_37 AC 108 ms
76,632 KB
testcase_38 AC 117 ms
76,664 KB
testcase_39 AC 116 ms
76,580 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