codeforces16B

本文详细解析了CodeForces平台上的经典算法问题'Burglar and Matches',通过贪心算法策略,解决了一个关于如何在限定条件下最大化背包中物品总价值的问题。代码实现清晰,提供了完整的解决方案。

Burglar and Matches

 CodeForces - 16B 

A burglar got into a matches warehouse and wants to steal as many matches as possible. In the warehouse there are m containers, in the i-th container there are ai matchboxes, and each matchbox contains bi matches. All the matchboxes are of the same size. The burglar's rucksack can hold n matchboxes exactly. Your task is to find out the maximum amount of matches that a burglar can carry away. He has no time to rearrange matches in the matchboxes, that's why he just chooses not more than nmatchboxes so that the total amount of matches in them is maximal.

Input

The first line of the input contains integer n (1 ≤ n ≤ 2·108) and integer m (1 ≤ m ≤ 20). The i + 1-th line contains a pair of numbers ai and bi (1 ≤ ai ≤ 108, 1 ≤ bi ≤ 10). All the input numbers are integer.

Output

Output the only number — answer to the problem.

Examples

Input
7 3
5 10
2 5
3 6
Output
62
Input
3 3
1 3
2 2
3 1
Output
7

sol:显然取个数多的,然后直接按题意贪心模拟即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=100005;
int n,m;
struct Match
{
    int Ges,Cnt;
}Box[N];
inline bool cmp_Cnt(Match p,Match q)
{
    return p.Cnt>q.Cnt;
}
int main()
{
    int i,ans=0;
    R(n); R(m);
    for(i=1;i<=m;i++)
    {
        R(Box[i].Ges); R(Box[i].Cnt);
    }
    sort(Box+1,Box+m+1,cmp_Cnt);
    for(i=1;i<=m&&n;i++)
    {
        ans+=min(n,Box[i].Ges)*Box[i].Cnt;
        n-=min(n,Box[i].Ges);
    }
    Wl(ans);
    return 0;
}
/*
Input
7 3
5 10
2 5
3 6
Output
62

Input
3 3
1 3
2 2
3 1
Output
7
*/
View Code

 

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10787440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值