C. Dima and Salad
time limit per test1 second
memory limit per test 256 megabytes
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 n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integers a1, 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.
Examples
input
Copy
3 2 10 8 1 2 7 1
output
Copy
18
input
Copy
5 3 4 4 4 4 4 2 2 2 2 2
output
Copy
-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[i]和b[i],求一个方案满足上述函数式,若没有方案,输出-1。
思路:因为要求(a[1]+a[2]+...a[t])/(b[1]+...b[t])=k,变换一下公式得(a[1]-k*b[1])+...(a[t]-k*b[t])=0,将问题转化为这n个物品中选若干个使其(a[1]-k*b[1])+...(a[t]-k*b[t])=0,求最大(a[1]+...a[t])最大。由于上面(a[m]-k*b[m])可以是负的,所以不能直接dp,要将等式左右同时都加上一个比较大的值,使其可以进行dp,然后dp[i][j]=max(dp[i-1][j],dp[i-1][j-(a[i]-k*b[i])]+a[i])。
细节:
1,由于dp定义的是前 i 个放进体积为 j 的背包最大值,要判断是否正好存入要有将dp赋值一个特小的负数,将dp[maxn]=0(maxn表示整体加maxn)。
2,整体右移可以取值的最小值(-maxn,maxn),这样0就是maxn的答案。
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <math.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <string.h>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define exp exp(1.0)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
const int maxn=200000;
int a[105],b[105];
int dp[105][maxn+100];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
memset(dp,-0x3f,sizeof(dp));
dp[0][100000]=0;
for(int i=1;i<=n;i++)
{
int w=a[i]-k*b[i];
for(int j=maxn;j>=0;j--)
{
if(j-w>=0&&j-w<=maxn)
{
dp[i][j]=max(dp[i-1][j-w]+a[i],dp[i-1][j]);
}
}
}
if(dp[n][100000]==0) dp[n][100000]=-1;
printf("%d\n",dp[n][100000]);
}
}