您的位置:首页 > 其它

Codeforces 526C - Om Nom and Candies(贪心)

2018-02-01 20:54 447 查看
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).

Output

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

Examples

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.

题意:你最多可以吃C千克的糖, 有两种糖,每种糖有两个参数,一个为重 w ,一个为欢乐度 h , 如何选择才能拥有最高的欢乐度, 两种糖数量不限。

解析:贪心做,我们选性价比最高的,尽量装这个物品,然后减少这个物品的空间(减少时是有优化的,不是一个一个减),分给第二个,当整个c被装满时,就退出循环就好了

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+100;
ll arr
;
map<ll,int>mp;
int c;
ll put(int a,int b,int e,int d)
{
ll pr2=c%a;
ll pr1=c-pr2;
ll mx=0;
while(pr1>=0&&pr2<=c)
{
if(!mp[pr2%e])
mp[pr2%e]=1;
else
break;
mx=max(mx,pr1/a*b+pr2/e*d);
if(pr2%e==0)
break;
//cout<<pr1<<' '<<pr2<<' '<<mx<<endl;
ll di;
if((e-pr2%e)%a!=0)//一种优化,防止超时
{
di=(e-pr2%e)/a+1;
}
else
di=(e-pr2%e)/a;
pr1-=di*a,pr2+=di*a;
}
return mx;
}
int main()
{
int h1,h2,w1,w2;
cin>>c>>w1>>w2>>h1>>h2;
double a=(double)w1/h1,b=(double)w2/h2;
if(a>=b)
{
cout<<put(h1,w1,h2,w2)<<endl;
}
else
cout<<put(h2,w2,h1,w1)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: