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. 数据挖掘经典算法之KNN

    KNN也称为k近邻算法,本质思想:物以类聚. 在分类或者预测中,待分类或预测的样本的类别和走势将直接参考与该样本最“近邻”的k个邻居. 在这种思路下,KNN注定会遇到3个问题: (1): 谁是我的邻居 ...

  2. 一款超出你想象的代码审阅软件understand

    看源码人们一般会想到source insight这款软件可是这款软件目前只支持windows平台,那如果我想在Linux平台上审阅代码呢, 没关系还有一款强大的软件understand,这款软件能够生 ...

  3. Unity Easy Save简单实用

    Easy Save使用: 1.保存游戏进度        2.设计游戏关卡(怪物数量,坐标,背景图等等) Easy Save默认存储地址: C:\Users\Administrator\AppData ...

  4. 设置Android设备在睡眠期间始终保持WLAN开启的代码实现

    MainActivity例如以下: package cc.ab; import android.os.Bundle; import android.provider.Settings; import ...

  5. Multiscale Combinatorial Grouping 学习和理解源代码(一)

    目标探测由于所做的最新研究.因此,这一领域的一般阅读文章.发现这篇文章,效果是比较新的比较好.在如此仔细研究.贴纸和共享.下面已经发布若干个连续的,分别对论文和代码进行大致地介绍,最后依据自己的实验对 ...

  6. ASCII码表完整版

        ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a ...

  7. python 笔记1--基础类型

    list 操作 append() 添加最外面 insert(pos,content) 插入指定地方 pop() 删除最外面 pop(pos) 删除指定地方 list中可以有list,且能用二维数组的方 ...

  8. 动态加载Ribbon功能区

    上下文选项卡对新UI的功能提供了极大的推进作用.当用户对某对象执行特定的任务时就会出现特定的选项卡.例如,在Excel中处理图表时,一个上下文选项卡提供用于图表处理的额外选项.本文将介绍创建和执行这些 ...

  9. c# 调用EXCEL在VS上能正常运行,部署在IIS上不能实现,在VS中运行页面和发布之后在IIS中运行的区别

    发现一篇文章,很好,解决了这个问题:感谢原博主!特此做个笔记. 地址:http://www.cnblogs.com/zhongxinWang/p/3275154.html 发布在IIS上的Web程序, ...

  10. 利用ajax做的柱状图,线性统计图,饼状图

    柱状图,两个不同类型的数据 以下是html页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...