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.

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int INF =; int dp1[];
int dp2[];
int a[];
int b[];
int Max(int x,int y)
{
return x>y? x:y;
}
void solve(int n,int m)
{
int i,j,k,hxl=-;
for(i=;i<=n;i++)
{
k=a[i]-b[i]*m;
if(k>)
{
for(j=;j>=k;j--)
{
dp1[j]=Max(dp1[j],dp1[j-k]+a[i]);
}
}
else
{
for(j=;j>=-k;j--)
{
dp2[j]=Max(dp2[j],dp2[j+k]+a[i]);
}
}
}
for(j=;j>=;j--)
{
k=dp1[j]+dp2[j];
if(k>hxl)
hxl=k;
}
if(hxl==)printf("-1\n");
else printf("%d\n",hxl);
}
int main()
{
int n,m;
int i;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
scanf("%d",&b[i]);
for(i=;i>=;i--)
{
dp1[i]=-INF;
dp2[i]=-INF;
}
dp1[]=;
dp2[]=;
solve(n,m);
}
return ;
}

CF Dima and Salad 01背包的更多相关文章

  1. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  2. CodeForces - 366C Dima and Salad (01背包)

    题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...

  3. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

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

  4. Dima and Salad(完全背包)

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

  5. Codeforces 366C Dima and Salad:背包dp

    题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...

  6. CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题

    题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...

  7. Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

    C. Dima and Salad   Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...

  8. codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费

    codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法, ...

  9. Codefroces 366 C Dima and Salad(dp)

    Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表 ...

随机推荐

  1. bzoj1875 HH去散步

    题目链接 一开始特别天真, $Folyd$传递闭包写了一下过了样例就交上去了 然后$gg$ $qwq$ 想了想$……$ 还要写一点东西 最后统计答案的时候有细节要注意 /*************** ...

  2. BZOJ4032: [HEOI2015]最短不公共子串(后缀自动机+序列自动机)

    题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...

  3. ArchLinux下十八岁的感悟

    这次不写技术文章,谈谈自己.现在我们之间没有博主和来访者的关系,我们就是简单聊聊天.你若愿意,就听我说一说. 或者你可以按下crtl+w退出该页面,博主有其他的技术文章.但我希望你能看看最好可以给我留 ...

  4. char * 与char []探究理解

    问题引入 以前一直认为二者是一样的,今天突然发现他们还是有很大的不同的.例如char *a = "abc"和char b[] = "abc",当我使用strca ...

  5. iOS8 dismissViewControllerAnimated:YES在Show Segue不工作

    最近使用iOS8 Sdk(xcode6.1.1)使用Show Segue功能,V_A视图push到V_B视图,然后想通过使用按键返回V_A,我记得ios6的时候可以使用[self dismissVie ...

  6. Kettle 解决数据锁的问题(事务(进程 ID 51)与另一个进程被死锁在 锁 资源上)

    1.Kettle做了一个作业, 执行的时候问题发生在步骤2和步骤3之间,也就是步骤2还未完全执行完的时候,步骤3就要更新步骤2插入的数据,造成死锁.(我的理解是既然都分开作业了,那么每个作业都是一个单 ...

  7. JQuery的get、post、ajax方法

    1.jQuery $.get() 方法 $.get() 方法通过 HTTP GET 请求从服务器上请求数据.  jQuery.get( url, [data], [callback] ):   参数: ...

  8. SD341X-SD343H蜗轮传动伸缩蝶阀厂家,SD341X-SD343H蜗轮传动伸缩蝶阀价格 - 专题栏目 - 无极资讯网

    无极资讯网 首页 最新资讯 最新图集 最新标签   搜索 SD341X-SD343H蜗轮传动伸缩蝶阀 无极资讯网精心为您挑选了(SD341X-SD343H蜗轮传动伸缩蝶阀)信息,其中包含了(SD341 ...

  9. jQuery 学习笔记(jQuery: The Return Flight)

    第一课. ajax:$.ajax(url[, settings]) 练习代码: $(document).ready(function() { $("#tour").on(" ...

  10. (转)虚拟路由器冗余协议【原理篇】VRRP详解

    原文:http://blog.51cto.com/zhaoyuqiang/1166840 为什么要使用VRRP技术 我们知道,为了实现不同子网之间的设备通信,需要配置路由.目前常用的指定路由方法有两种 ...