『题解』A79.装箱问题
2025-06-07 21:41:46
发布于:湖南
0阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re return
#define ew 5005
#define sz 10000005
const int MOD=1e9+7;
int a[sz];
int dp[35][20005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int v;//容量
cin>>v;
int n;//个数
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];//价值
}
for(int i=1;i<=n;i++){
for(int j=0;j<=v;j++){
if(j>=a[i]){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+a[i]);
}
else{
dp[i][j]=dp[i-1][j];
}
}
}
cout<<v-dp[n][v];
re 0;
}
典型的背包问题,动归问题,原题在ybt
这里空空如也
有帮助,赞一个