AcWing 1024. 装箱问题
原题链接
简单
作者:
minux
,
2020-05-09 11:22:38
,
所有人可见
,
阅读 535
c++
#include <bits/stdc++.h>
using namespace std;
const int N=20005;
int f[N];
int v, n;
int main(){
memset(f, 0x00, sizeof f);
cin>>v;
cin>>n;
int w;
for(int i=0; i<n; ++i){
cin>>w;
for(int j=v; j>=w; --j) f[j]=max(f[j], f[j-w]+w);
}
cout<<v-f[v]<<endl;
return 0;
}
python
class Solution:
def main(self, n:int, v:int):
f = [0 for _ in range(v+1)]
for _ in range(n):
w=int(input())
for j in range(w, v+1)[::-1]:
f[j]=max(f[j], f[j-w]+w)
return v-f[v]
if __name__ == '__main__':
v, n=int(input()), int(input())
sol=Solution()
print(sol.main(n, v))