0-1背包问题
python 100分 一维数组
n,x=list(map(int,input().split(" ")))
arr=[int(input()) for i in range(n)]
sums=sum(arr)
dp=[0]*(sums+1)
for i in range(n):
for j in range(sums,arr[i]-1,-1):
dp[j]=max(dp[j],dp[j-arr[i]]+arr[i])
for i in range(sums+1):
if dp[i]>=x:
print(dp[i])
break
python 100分 二维数组
n,x=list(map(int,input().split(" ")))
arr=[int(input()) for i in range(n)]
arr.insert(0,0)
sums=sum(arr)
dp=[[0]*(sums+1) for i in range(n+1)]
for i in range(1,n+1):
for j in range(sums+1):
if j>=arr[i]:
dp[i][j]=max(dp[i-1][j],dp[i-1][j-arr[i]]+arr[i])
else:
dp[i][j]=dp[i-1][j]
for i in range(sums+1):
if dp[n][i]>=x:
print(dp[n][i])
break