多个基础的01背包

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 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

给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量)。
要保证在所选的a[i]的总和比上b[i]的总和等于k前提下,求出最大的ai和。

代码:
 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e4+20, maxw=1e5+20;
const int INF=1e5+20;
int cal[maxn], tas[maxn], n, k;
int f[maxw], g[maxw];

int main(void){
    while (scanf("%d%d", &n, &k)==2){
        for (int i=1; i<=n; i++) scanf("%d", &tas[i]);
        for (int i=1; i<=n; i++) scanf("%d", &cal[i]);

        for (int i=0; i<maxw; i++)
            f[i]=g[i]=-INF;
        f[0]=g[0]=0;
        for (int i=1; i<=n; i++){
            int cost=tas[i]-k*cal[i], val=tas[i];
            if (cost<0){
                cost*=-1;
                for (int j=maxw-1; j>=cost; j--)
                    f[j]=max(f[j], f[j-cost]+val);
            }else{
                for (int j=maxw-1; j>=cost; j--)
                    g[j]=max(g[j], g[j-cost]+val);
            }
        }

        int ans=0;
        for (int i=0; i<maxw; i++)
            ans=max(ans, f[i]+g[i]);
        printf("%d\n", (ans==0)?-1:ans);
    }

    return 0;
}

 

可得:∑aj=k*∑bj ( j:[ 1,m ] );

那么,我们创造一个数组 g[ i ]=a[ i ]-k*b[ i ]; 
于是乎,这个问题就转化成了,挑选一种或多种水果,使得 ∑g[ i ]=0,并且使对应的 ∑ai 最大。 
是不是熟悉起来了? 
再用另一种语言表述:挑选若干个物品,使得其体积( g )为0,并保证其价值总和( a )最大。 
好,显而易见了,裸的01背包问题。

然而,我们发现g[ i ]可能是负数,为了解决c++中万恶的下标不能为负的问题,我们将整个数组向右平移,即下标全部加上某个数 p 以保证它为正,结合本题数据规模,我的 p =100000;

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k;
int v[1100],w[1100];//前者为a,后者为b。
int g[1100];
int p=100000;
int x,y;
int f[2000000];

void init()
{
    x=0;y=0;
    int s=0;
    for(int i=1;i<=n;++i) cin>>v[i];
    for(int i=1;i<=n;++i) cin>>w[i],g[i]=v[i]-w[i]*k,s+=g[i];
    x=y=s;
    for(int i=1;i<=n;++i) x=min(x,x-g[i]),y=max(y,y-g[i]);//x、y分别表示所取g[ i ]之和可能达到的最小值&最大值,用于dp时枚举的上下界。
}

void work_dp()
{
    memset(f,-1,sizeof(f));//由于我们强制要求最终体积=0,而我们并不能保证这种情况一定合法存在,所以初始状态是全都不合法。
    f[p]=0;//f[ 0 ]=0平移后的产物。
    for(int i=1;i<=n;++i)
    {
        if(g[i]>=0)//这里分类了。因为g[i]>=0时,只有倒着枚举才能保证f[j-g[i]+p]没有被上一层更新过,这时和01背包是一样的。
        {
            for(int j=y;j>=x;--j)
                if(f[j-g[i]+p]>=0) f[j+p]=max(f[j+p],f[j-g[i]+p]+v[i]);
        }
        else for(int j=x;j<=y;++j)//但是如果g[i]<0,那么如果仍然倒推,由于j-g[i]+p在j+p的右侧,而右侧是已更新过的值,那么就会出错,所以应该顺推。
            if(f[j-g[i]+p]>=0) f[j+p]=max(f[j+p],f[j-g[i]+p]+v[i]);
    }//其实这里j枚举的上下界比较粗糙,严格地来说,还应满足j-g[i]+p也在[x,y]内,不过反正是影响不到结果的,就没怎么仔细改了。
    if(f[p]) printf("%d",f[p]);
    else printf("-1");
}
int main()
{
    cin>>n>>k;
    init();
    work_dp();
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值