ZeptoLab Code Rush 2015/Codeforces 526C Om Nom and Candies(贪心+思维)

6 篇文章 0 订阅

题面

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.

题目链接

CodeForces 526C

参考链接

ZeptoLab Code Rush 2015---C. Om Nom and Candies author:tokers

题目简述

小怪兽吃红糖果和蓝糖果,
一颗蓝糖果重量为weight_blue,可以收获happy_blue的快乐
一颗红糖果重量为weight_red,可以收获happy_red的快乐
最多可以吃C重量的糖果,问最多可以获得多少快乐
试图遍历其中某种糖果数量,来获得最大值,但是难以接受O(C)的复杂度


分情况讨论:
①当其中一种糖果的重量较大,C/weight<=sqrt(C)(循环次数小于等于sqrt(C))时,可以遍历该种糖果的数量获得最大值
②当两种糖果的重量都较小,C/weight>sqrt(C)(循环次数大于sqrt(C))时,
若happy_red/weight_red>happy_blue/weight_blue时,
happy_red*weight_blue>happy_blue*weight_red,吃蓝糖果不如吃红糖果。
试图遍历蓝糖果的数量,每到达lcm(weight_blue,weight_red)重量,就把这些重量的花费全都拿去吃红糖果,
则最终实际蓝糖果数量不超过lcm(weight_blue,weight_red)/weight_blue,
即循环次数<=lcm(weight_blue,weight_red)/weight_blue<=weight_red<=sqrt(C);


综上所述循环次数到达sqrt(C),就可以满足以上两种情况。

程序

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
    long long C,weight_red,weight_blue,happy_red,happy_blue;
    scanf("%lld%lld%lld%lld%lld",&C,&happy_red,&happy_blue,&weight_red,&weight_blue);
    long long answer=0;
    for(int i=0;i<=sqrt(C);i++)
    {
        if(i*weight_blue<=C)
            answer=max(answer,i*happy_blue+(C-i*weight_blue)/weight_red*happy_red);
        if(i*weight_red<=C)
            answer=max(answer,i*happy_red+(C-i*weight_red)/weight_blue*happy_blue);
    }
    printf("%lld\n",answer);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值