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.

题意:

给两个数组:a,b。然后让你从a数组中选取任意的几个数字,得到的和为sum1,然后除以相应b数组和sum2,要求sum1/sum2==k,求出满足要求的a数组中sum1最大的为多少,不存在输出-1。

思路:

其实这题就是背包变形,将 ai-k*bi 看做重量,将 ai 看做价值,总容量为 0 ,进行背包,最后满足条件的情况为背包恰好装满的情况,答案即为dp[0]。

因为 ai-k*bi 可以为负数,所以要将原点平移,可以预算一下, 原点到10000就够了,剩下的就是01背包的问题了,还需注意一点的是 重量为正和负的要分开考虑,WA了我好久才想明白。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 300005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; int n,m,ans,k,flag,cnt;
int a[maxn],b[maxn];
int w[maxn];
int dp[MAXN]; bool solve()
{
int i,j;
memset(dp,-INF,sizeof(dp));
dp[10000]=0;
for(i=1; i<=n; i++)
{
if(w[i]>=0)
{
for(j=20000; j>=w[i]; j--)
{
if(dp[j-w[i]]!=-INF) dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
else
{
for(j=0;j<20000;j++)
{
if(dp[j-w[i]]!=-INF) dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
}
ans=dp[10000];
if(ans==0) return false ;
return true ;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&k))
{
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(i=1; i<=n; i++)
{
scanf("%d",&b[i]);
}
for(i=1; i<=n; i++)
{
w[i]=a[i]-k*b[i];
}
if(solve()) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

最新文章

  1. iOS 学习 - 3.仿qq列表
  2. RevMan简单入门指南
  3. 第16章 调色板管理器_16.4 一个DIB位图库的实现(1)
  4. 变形--缩放 scale()
  5. EXT3_DX_ADD_ENTRY: DIRECTORY INDEX FULL!
  6. lintcode:Minimum Subarray 最小子数组
  7. C++调用matlab实例
  8. 教你50招提升ASP.NET性能(四):精选的技巧
  9. C++中单链表的建立和操作
  10. UESTC_贪吃蛇 CDOJ 709
  11. MFC模式对话框与非模式对话框 消息处理顺序
  12. ACdream 1148(莫比乌斯反演+分块)
  13. win32 安装 xcache扩展
  14. RabbitMQ小白菜学习之在window下的安装配置
  15. 读书笔记 effective c++ Item 44 将与模板参数无关的代码抽离出来
  16. MariaDB存储过程笔记
  17. Selenium断言的使用,等待
  18. CentOS下的Mysql的安装和使用
  19. HDU 5355 Cake (构造 + 暴力)
  20. BZOJ.1812.[IOI2005]Riv 河流(树形背包)

热门文章

  1. BZOJ 2654: tree( 二分 + MST )
  2. Java面试题集(1-50)
  3. USACO Healthy Holsteins DFS
  4. 动态加载EXE和DLL
  5. C#操作Excel(读/写)
  6. KINGSO介绍
  7. 编译cm12.1
  8. tweenanim动画
  9. 01-Foundation简介、NSObject、copy、NSString
  10. 知识点1-3:MVC设计模式