Codeforces 526c-om Nom and Candies

Source: Internet
Author: User

A Sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation this required him to think a bit on order to enjoy candies the most. Would 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 the bags with candies. The first is full of blue candies and the second bag is full of red candies. Om Nom knows that each red candy weighs W R grams and each blue candy Weighs W b grams. Eating a single red candy gives Om nom H R Joy units and eating a single blue candy gives Om nom H b joy units.

Candies was the most important thing in the world, but on the other hand overeating was not good. Om Nom knows if he eats more thanC grams of candies, he'll get sick. Om Nom thinks that it's 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 this each bag contains more candies that Om Nom can eat.

Input

The single line contains five integers C,? HR,? Hb,? WR,? Wb (1?≤? C,? H R,? H b,? W R,? W b. ≤?109).

Output

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

Examplesinput
10 3 5) 2 3
Output
16
Note

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


Test instructions: Infinite knapsack problem, there are only two kinds of goods, the first thought can do three points. Then crazy WA.


Analysis: Violent enumeration of the number of two commodities, when Max (WA,WB) < sqrt (c), WA sugar B can be changed to the WB sugar A, adjust the solution must be better, so the most enumerated to sqrt (c),


#include <cstdio> #include <iostream>using namespace Std;long long C,hr,hb,wr,wb,ans;long long check (long Long x) {return x*hr + (C-X*WR)/WB*HB;} int main () {Cin.sync_with_stdio (false);cin>>c>>hr>>hb> >wr>>wb;for (int i = 0;i <= min (C/WR,100000LL); i++) ans = max (Ans,check (i)), swap (HR,HB), swap (WR,WB); for (int i = 0;i <= min (C/WR,100000LL); i++) ans = max (Ans,check (i));cout<<ans<<endl; }


Codeforces 526c-om Nom and Candies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.