您的位置:首页 > 其它

CF 526C Om Nom and Candies

2015-04-06 01:54 405 查看

C. Om Nom and Candies

time limit per test 1 second

memory limit per test 256 megabytes

input standard input

output standard output

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?



One day, when he came to his friend Evan, Om Nom didn’t find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.

Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn’t proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.

Input

The single line contains five integers C, Hr, Hb, Wr, Wb(1 ≤ C, Hr, Hb, Wr, Wb ≤ 109)C, H_r, H_b, W_r, W_b (1 ≤ C, H_r, H_b, W_r, W_b ≤ 10^9).

Output

Print a single integer — the maximum number of joy units that Om Nom can get.

Sample test(s)

Input

10 3 5 2 3

Output

16

Note

In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.

解析

数学。分块。

1. 当Wr, Wb W_r, W_b任意一个大于c√\sqrt {c}时(假设Wr>c√\sqrt {c})枚举红糖的个数[0,c/WrW_r]枚举量是O(c√\sqrt c)的

2. 当 Wr, Wb W_r, W_b都小于c√\sqrt c时(假设HrWr>HbWb\frac {H_r}{W_r}>\frac {H_b}{W_b})可以推出不等式Hr⋅Wb>Hb⋅WrH_r \cdot W_b > H_b \cdot W_r

这里可以推导出来一个重要的结论B(表示blue的数量)一定小于WrW_r

证明

若B>=Wr那么一定可以写成B=Wr+x的形式

joy=B Hb + R Hr

=(Wr+x) * Hb + R * Hr

=x * Hb + Wr*Hb + R * Hr

而前面有个前提:Hb * Wr < Hr * Wb

所以

joy < x * Hb + Hr * Wb + R * Hr=x * Hb + (Wb+R) * Hr

如果x是正数,那么x个blue (Wb+R)个red是更的优解。

与假设矛盾,所以B < Wr

因为Hr/Wr>Hb/Wb那么显然红糖比蓝糖多 R > B

c > B* Wb+R* Wr >B* Wb+B* Wr> B* Wb+B* B

故有 c > B*B=> B < sqrt(c)

#include<cstdio>
#include<algorithm>
#include<cmath>

using namespace std;

typedef long long LL;
int c,hr,hb,wr,wb;

void way1()
{
if(wr<wb) {swap(hr,hb); swap(wr,wb);}
long long ans=0;
for(int i=0;i*wr<=c;i++)//i->red
{
int b=(c-i*wr)/wb;
ans=max(ans,(long long)hr*i+(long long)hb*b);
}
printf("%I64d\n",ans);
}

void way2()
{
if((double)hr/(double)wr<(double)hb/(double)wb)
{swap(hr,hb); swap(wr,wb);}
long long ans=0;
for(int i=0;i<=wr;i++)//i->blue
{
int r=(c-i*wb)/wr;
ans=max(ans,(long long)hr*r+(long long)hb*i);
}
printf("%I64d\n",ans);
}

int main()
{
scanf("%d%d%d%d%d",&c,&hr,&hb,&wr,&wb);
if(max(wr,wb)>sqrt((double)c)) way1();
else way2();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: