16BBurglar and Matches

来源:互联网 发布:软件比赛有哪些 编辑:程序博客网 时间:2024/05/19 20:23

B. Burglar and Matches
time limit per test
0.5 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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 n matchboxes 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 35 102 53 6
output
62
input
3 31 32 23 1
output
7


题意:给你n个你需要带走的箱子,m个大集装箱,每个集装箱里有a个盒子,每个盒子里面有b根火柴。输出能拿走n个盒子的火柴最大数。

题解:贪心,用结构体排序每个盒子里面最大的火柴数,每次拿最多火柴的火柴盒,用n减去拿掉的,直到n为0.最开始还想前缀和,很麻烦。看了一下博客都是贪心,就贪心咯。

#include<bits/stdc++.h>using namespace std;struct node{    int a;    int b;} q[25];bool cmp(node a,node b){    return a.b>b.b;}int main(){    int n,m;    long long ans;    while(cin>>n>>m)    {        ans=0;        for(int i=0; i<m; i++)            cin>>q[i].a>>q[i].b;        sort(q,q+m,cmp);        for(int i=0; i<m; i++)        {            if(n>=q[i].a)            {                ans+=q[i].a*q[i].b;                n-=q[i].a;            }            else            {                ans+=n*q[i].b;                break;            }        }        cout<<ans<<endl;    }    return 0;}