View Code of Problem 3199

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<map>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 2000000;
int dp[maxn];
int sum=0;
int volume[maxn], value[maxn], c[maxn];
int n, v; // 总物品数,背包容量量
//val 最大容量,val 总价值,amount 数量
// 01 背包
void ZeroOnepark(int val, int vol) {
	for (int j = sum ; j >= vol; j--)
		dp[j] = max(dp[j], dp[j - vol] + val);
}
int m;
int main(){
	while(~scanf("%d %d",&n,&m)){
		sum=0;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			scanf("%d",&volume[i]);
			sum+=volume[i];
		}
		for(int i=1;i<=n;i++){
			ZeroOnepark(volume[i],volume[i]);
		}
		
		int t;
    	for(int i=1;i<=sum;i++)
		{
        	if(dp[i]>=m)
			{

            	t=dp[i]-m;
				break;
        	}
    	}
    	printf("%d\n",t);
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 3199