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. [AIR] 与本地进程(应用程序)进行通讯

    毫不夸张的说,此功能可以将Windows命令行程序(cmd.exe)通过AIR应用封装起来,并指挥它做任何你想做的事情 AIR2.0及以上与本地进程的交互主要体现在以下几方面: 启动进程并提供初始参数 ...

  2. P4850 [IOI2009]葡萄干raisins 记忆化搜索

    $ \color{#0066ff}{ 题目描述 }$ 普罗夫迪夫的著名巧克力大师Bonny需要切开一板带有葡萄干的巧克力.巧克力是一个包含许多相同的方形小块的矩形.小块沿着巧克力的边排列成n行m列,即 ...

  3. EndNote登陆Web账号时解决不断询问用户名密码

    EndNote登陆Web账号时不断询问用户名密码怎么破?EndNote有个同步功能,在登陆Web账号时,EndNote不断的询问账号和密码,可是用户名和密码明明已经填写正确和完毕,就是登陆不上EndN ...

  4. [BZOJ 4890][TJOI2017]城市

    传送门 $ \color{green} {solution : }$ 我们可以暴力枚举断边,然后 $ O(n) $ 的跑一次换根 $ dp $,然后复杂度是 $ O(n * n) $ 的 #inclu ...

  5. Pycharm快捷键配置

    1.主题 毫无疑问Pycharm是一个具有强大快捷键系统的IDE,这就意味着你在Pycharm中的任何操作,例如打开一个文件.切换编辑区域等,都可以通过快捷键来实现.使用快捷键不仅能提高操作速度,看起 ...

  6. 最近总想着写一个模拟alert和confirm插件,代替原生的

    msgbox-confirm github:  https://github.com/tong-mikasa/msgbox-confirm 主要js代码,scss代码 (function($) { $ ...

  7. C#嵌入字体

    1:将字体文件(*.ttf)添加到资源文件里,注意资源的名称 2:在Program.cs文件里声明 using System; using System.Collections.Generic; us ...

  8. iozone文件系统测试工具在AM335x上的移植

     IOzone下载    下载地址:http://www.iozone.org 如下: 解压iozone,并进入到解压路径下的src/current 我的是  iozone3_487 命令:cd  i ...

  9. jQuery懒加载插件 – jquery.lazyload.js

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  10. 《大数据日知录》读书笔记-ch3大数据常用的算法与数据结构

    布隆过滤器(bloom filter,BF): 二进制向量数据结构,时空效率很好,尤其是空间效率极高.作用:检测某个元素在某个巨量集合中存在. 构造: 查询: 不会发生漏判(false negativ ...