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=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
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=;
R(n); R(m);
for(i=;i<=m;i++)
{
R(Box[i].Ges); R(Box[i].Cnt);
}
sort(Box+,Box+m+,cmp_Cnt);
for(i=;i<=m&&n;i++)
{
ans+=min(n,Box[i].Ges)*Box[i].Cnt;
n-=min(n,Box[i].Ges);
}
Wl(ans);
return ;
}
/*
Input
7 3
5 10
2 5
3 6
Output
62 Input
3 3
1 3
2 2
3 1
Output
7
*/
 

codeforces16B的更多相关文章

随机推荐

  1. [Javascript] encodeURIComponent()方法

    在vue項目中使用vue-router通过url进行传值 encodeURIComponent encodeURIComponent() 函数可把字符串作为 URI 组件进行编码.encodeURIC ...

  2. JVM的总结

    1.JVM的内存模型 JVM主要由程序计数器,虚拟机栈,堆,方法区,本地方法区 1.程序计数器的功能是记录当前线程执行到了字节码文件的哪一行, JVM执行的是.java编译后的.class文件 2.虚 ...

  3. MyBatis入门简述

    MyBatis前身是iBatis,为Apache的一个开源项目.2010年迁移到了Google Code,改名为MyBatis.2013年迁移到Github. MyBatis是一个优秀的持久层框架,它 ...

  4. Ubuntu16.04下搭建mysql + uwsgi + nginx环境启动flask 项目

    1.安装mysql Sudo apt-get install mysql 配置mysql的数据存储路径,默认在 /var/lib/mysql sudo cp -R /var/lib/mysql/* / ...

  5. 我带着小程序和Springboot后端终于战胜了WebSocket!!!胜利( •̀ ω •́ )y

    WebSocket项目笔记 1. What is WebSocket? (以下内容来源于百度百科) WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器 ...

  6. java笔记---- 获取外网(公网)的ip地址

    import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import ...

  7. VS2017的MVC和Angular联合开发的配置文件作用

    在通过MVC和Angular联合开发项目时,项目里有几个重要的配置文件,下面列出这几个配置文件的分析和比较: 主要配置文件有appsettings.json,tsconfig.json,package ...

  8. About A Scam

    事件起因 本篇记录一个我遇到一个诈骗故事. 这两年我陆续有收到邮件,内容为有一大笔遗产我可以继承,让我提供银行卡号,身份证号相关信息. 后面邮件的内容就变为,有一笔公益款项,可以用我名义去管理,让我提 ...

  9. 在Windows 10 x64 编译ReactOS-0.4.5源码并在VMare中运行

    1.首先下载ReactOS源码(版本是0.4.5,最新版本0.4.9暂没有编译),然后下载RosBe(版本是2.1.6) 2.将下载好的ReactOS源码包放到指定磁盘的文件夹中,目录路径为英文(重要 ...

  10. 初窥css---盒子以及盒子扩展

    盒子以及盒子扩展 盒子 盒子是用来实现将网页区域化的一个非常重要的工具,盒子使得网页各部分十分清晰的被分开,对于程序员十分友好(...),并且使得网页更加容易维护. 盒子的常用属性 宽和高这两个属性就 ...