結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,560 KB
testcase_01 AC 46 ms
58,852 KB
testcase_02 AC 41 ms
52,880 KB
testcase_03 AC 46 ms
59,532 KB
testcase_04 AC 40 ms
52,944 KB
testcase_05 AC 78 ms
65,340 KB
testcase_06 AC 145 ms
65,524 KB
testcase_07 AC 116 ms
65,040 KB
testcase_08 AC 71 ms
64,764 KB
testcase_09 AC 41 ms
52,524 KB
testcase_10 AC 149 ms
67,440 KB
testcase_11 AC 99 ms
66,604 KB
testcase_12 AC 81 ms
65,944 KB
testcase_13 AC 150 ms
66,168 KB
testcase_14 AC 68 ms
64,424 KB
testcase_15 AC 89 ms
65,948 KB
testcase_16 AC 46 ms
58,524 KB
testcase_17 AC 81 ms
67,532 KB
testcase_18 AC 84 ms
64,536 KB
testcase_19 AC 44 ms
59,592 KB
testcase_20 AC 72 ms
66,048 KB
testcase_21 AC 173 ms
67,004 KB
testcase_22 AC 109 ms
65,076 KB
testcase_23 AC 144 ms
65,492 KB
testcase_24 AC 59 ms
64,748 KB
testcase_25 AC 98 ms
64,596 KB
testcase_26 AC 97 ms
65,224 KB
testcase_27 AC 97 ms
65,580 KB
testcase_28 AC 93 ms
65,744 KB
testcase_29 AC 163 ms
65,088 KB
testcase_30 AC 41 ms
52,872 KB
testcase_31 AC 57 ms
64,796 KB
testcase_32 AC 62 ms
68,792 KB
testcase_33 AC 58 ms
64,552 KB
testcase_34 AC 65 ms
65,676 KB
testcase_35 AC 96 ms
64,808 KB
testcase_36 AC 46 ms
57,876 KB
testcase_37 AC 78 ms
65,972 KB
testcase_38 AC 85 ms
65,868 KB
testcase_39 AC 85 ms
64,824 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