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

题解:

做水果色拉,有n种水果,水果有两个属性:味道和卡路里,选一定水果,让求使得味道和除以卡路里和为k的时候的味道和的最大值;

刚开始我的想法是弄一个完全背包,可以得到多种组合;判断bag[j] / j == k即可;但是当味道和一定的时候可能存在多个卡路里和值,必然会对下面的数据造成影响;

看了大神的,是转化了下:c =a -b*k;可以转化成两个背包;正数存一个,负数存一个,以免造成当味道和一定的时候可能存在多个卡路里和值的情况;

ac

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void getdata(ref int x) {
x = ;
int t;
while (true) {
t = Console.Read();
if (t >= '' && t <= '') break;
}
while (true) {
if (t < '' || t > '') break;
x = x * + t - '';
t = Console.Read();
}
}
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b)
{
return a < b ? a : b;
}
static void Main(string[] args)
{
int[] a = new int[];
int[] b = new int[];
int[] bag1 = new int[];
int[] bag2 = new int[];
int n = , k = ;
getdata(ref n);
getdata(ref k);
//Console.Read();
for (int i = ; i < ; i++) {
bag1[i] = -;
bag2[i] = -;
}
bag1[] = ;
bag2[] = ;
int ans = , V = ;
for (int i = ; i < n; i++)
{
getdata(ref a[i]);
}
for (int i = ; i < n; i++) {
getdata(ref b[i]);
} for (int i = ; i < n; i++) {
int w = a[i] - b[i] * k;
if (w < )
{
w = -w;
for (int j = V; j >= w; j--)
{
bag1[j] = max(bag1[j], bag1[j - w] + a[i]);
}
}
else {
for (int j = V; j >= w; j--) {
bag2[j] = max(bag2[j], bag2[j - w] + a[i]);
}
}
}
for (int i = ; i >= ;i-- )
{
ans = max(ans, bag1[i] + bag2[i]);
}
if (ans == )
Console.WriteLine(-);
else
Console.WriteLine(ans);
// while (true) ;
}
}
}

Dima and Salad(完全背包)的更多相关文章

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

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

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

  3. CF Dima and Salad 01背包

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

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

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

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

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

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

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

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

  8. Codefroces 366 C Dima and Salad(dp)

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

  9. codeforces 366C Dima and Salad 【限制性01背包】

    <题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...

随机推荐

  1. centos directory server

    http://www.aliyun.com/zixun/content/3_12_517262.html CentOS系统安装Directory Server 8.1操作方法 发布时间:2014-12 ...

  2. SQL Server 中使用参数化Top语句

    在T-Sql中,一般top数据不确定的情况下,都是拼sql,这样无论是效率还是可读性都不好.应该使用下面参数化Top方式:declare @TopCount int set @TopCount = 1 ...

  3. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. More is better(并差集)

    More is better Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 327680/102400K (Java/Other) To ...

  5. sql 中的时间处理问题

    select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...

  6. SQL Server2008R2安装失败问题之语言包问题

           今天安装SQL Server2008 的时候出现了,如下的的问题,安装过程在ExcuteStandardTimingsWorkflow时候报错,结束安装.       提示:       ...

  7. 网页 php开发中html空文本节点问题user agent stylesheetbody

    最近开发中遇到一个奇怪的问题,我的一个网站头部,代码固定不变,放在了不同的模板进行展示,一部分出现了问题,总是距离相差8个像素,用firebug查看发现:meta 跑到 body 下面去了,并且发现了 ...

  8. C#事件、委托简单示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. IOS中设置状态栏的状态

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...

  10. bootstrap之 formgroup表单布局样式

    <form class="form-horizontal" role="form"> <fieldset> <legend> ...