ZeptoLab Code Rush 2015---C. Om Nom and Candies,zeptolab2015---c

來源:互聯網
上載者:User

ZeptoLab Code Rush 2015---C. Om Nom and Candies,zeptolab2015---c

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

如果某種糖的W值 ≥C−−√, 那麼可以直接枚舉這種糖的數量

如果2種糖的W值均 ≤C−−√, 這時我們假設, Hb⋅Wr<Hr⋅Wb
設藍色的糖拿了X個,且X≥Wr, 我們把X分為2份,X=Y+k⋅Wr, 由於 Hb⋅Wr<Hr⋅Wb,所以把k⋅Wr的那一部分拿來換成紅色的糖,性價比更高,因此,在這種情況下,藍色的糖的數目應該<Wr
Hb⋅Wr>Hr⋅Wb 和 Hb⋅Wr=Hr⋅Wb的情況同理可以推出

/*************************************************************************    > File Name: Z-C.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月05日 星期日 13時09分31秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int main(){    LL C, Hr, Hb, Wr, Wb;    while (cin >> C >> Hr >> Hb >> Wr >> Wb)    {        if (C < Wr && C < Wb)        {            cout << 0 << endl;            continue;        }        if (C < Wr && C >= Wb)        {            cout << C / Wb * Hb << endl;            continue;        }        if (C < Wb && C >= Wr)        {            cout << C / Wr * Hr << endl;            continue;        }        LL ans = 0;        for (LL i = 0; i * i <= C; ++i)        {            if (C - i * Wb >= 0)            {                ans = max(ans, i * Hb + (C - i * Wb) / Wr * Hr);            }            if (C - i * Wr >= 0)            {                ans = max(ans, i * Hr + (C - i * Wr) / Wb * Hb);            }        }        cout << ans << endl;    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.