【codeforce C】 Dima and Salad 01背包

版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86513224
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
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.

题目要求a的和为b的和的k倍,就是说最后要达到的状态为ai1+ai2+ai3+ai4==kbi1+kbi2+kbi3+kbi4……

不妨设ai=k*bi+c,c=k*bi-ai; 这里的c表示如果选了这组,那么还差多少才能满足题意。很显然,最后得到的结果中,所有的c的和应该为0.

定义dp[i]表示还差 i 可以满足题意时 最大能达到的 a (taste值)。

dp[j]=max(dp[j] , dp[j-c[i]]+a[i]);

注意c可能为正也可能为负,为了不使某一个fruit被重复加两次,可以开二维数组来做,也可以先判断c的正负,再根据正负来写 j 的遍历顺序,这样只用一维数组。

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
#define inf 2147480000
int n,k;
int dp[101][20002];
int box[101];
int aox[101];
int cox[101];
int main(int argc, char *argv[])
{
    cin>>n>>k;
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=n;i++)
        cin>>aox[i];
    for(int i=1;i<=n;i++)
        cin>>box[i];
    for(int i=1;i<=n;i++)
        cox[i]=k*box[i]-aox[i];
    dp[0][10000]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=20000;j>=0;j--)
        {
            if(j-cox[i]>=0 && j-cox[i]<=20000)
            {
                if(dp[i-1][j-cox[i]]!=-1)
                {
                    dp[i][j]=max(dp[i][j],dp[i-1][j-cox[i]]+aox[i]);
                }
            }
            dp[i][j]=max(dp[i-1][j],dp[i][j]);
        }
    }
    if(dp[n][10000]!=0)
        cout<<dp[n][10000]<<endl;
    else
        cout<<-1<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/86513224