http://codeforces.com/contest/366/problem/C之01背包

C. Dima and Salad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.

Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words,  , where aj is the taste of the j-th chosen fruit and bj is its calories.

Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!

Inna loves Dima very much so she wants to make the salad from at least one fruit.

Input

The first line of the input contains two integers nk (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integersa1, a2, ..., an (1 ≤ ai ≤ 100) — the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 100) — the fruits' calories. Fruit number i has taste ai and calories bi.

Output

If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.

Sample test(s)
input
3 2
10 8 1
2 7 1
output
18
input
5 3
4 4 4 4 4
2 2 2 2 2
output
-1
Note

In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition  fulfills, that's exactly what Inna wants.

In the second test sample we cannot choose the fruits so as to follow Inna's principle.

题意:n种水果,每种水果有两种属性a,b,需要在这n个水果中选出任意个数水果使得他们的 (a总和 )/(b总和) = k,求a[i]总和的最大值,如果不存在输出-1

分析:这里的难点是水果有两种属性,即a[i]和b[i]是关联的,但是由于k是定值,所以可以求出每种水果要使得a/b = k 需要a减少多少或者a增加多少

令c=a-b*k,那么可以把c作为这个水果所要占用的容量,所有c的总和作为背包容量,f[j]表示占用容量j所得到的a的总和的最大值,则题目转化为求f[0],具体看代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=2*100000+10;
int a[110],c[110],wa[MAX],wb[MAX],*f=&wa[102000],*g=&wb[102000];

int main(){
	int n,k,b;
	while(cin>>n>>k){
		int x=0,y=0,sum=0;
		for(int i=0;i<n;++i)cin>>a[i];
		for(int i=0;i<n;++i){
			cin>>b;
			c[i]=a[i]-b*k;//(a[i]-c[i])/b=k => c[i]=a[i]-b*k
			if(c[i]<0)x+=c[i];
			else y+=c[i];
		}
		for(int i=x-100;i<=y+1000;++i)f[i]=g[i]=-INF;
		//01背包,c[i]作为重量,a[i]作为价值 
		g[0]=0;
		for(int i=0;i<n;++i){
			for(int j=x;j<=y;++j){
				f[j]=g[j];
				f[j]=max(f[j],g[j-c[i]]+a[i]);//c[i]将a[i]和b[i]联系起来了 
			}
			sum=f[0]>sum?f[0]:sum;
			swap(f,g);
		}
		if(!sum)sum=-1;
		cout<<sum<<endl;
	}
	return 0;
}


你可能感兴趣的:(http://codeforces.com/contest/366/problem/C之01背包)