C. Om Nom and Candies(ZeptoLab Code Rush 2015)

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 Cgrams 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).

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.


题意:

     给出可吃最大糖果重量,和两种糖果的单个重量和单个快乐值,求其获得的最大快乐值。

题解:

    由于数据范围比较大,暴力枚举最坏情况下会超时,假如第一个的wr*wr>=c,那么肯定可以在sqrt(c)内枚举完,假如wr*wr<c,此时如果wb*wb>=c,那么就枚举wb,假如wb*wb也<c,那么我们就比较两个的单位克的权值,权值小的个数肯定不会大于sqrt(c)个。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
    long long c,hr,hb,wr,wb;
    while(cin>>c>>hr>>hb>>wr>>wb)
    {
        if(wr*wr<c&&((1.0*hr/wr>1.0*hb/wb)||wb*wb>=c))
        {
            swap(wr,wb);
            swap(hr,hb);
        }
        long long ans=0;
        for(long long i=0;i*i<=c&&wr*i<=c;i++)
        {
            ans=max(ans,hr*i+(c-wr*i)/wb*hb);
        }
        cout<<ans<<endl;
    }
    return 0;
}



你可能感兴趣的:(Algorithm,编程,ACM,codeforces)