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. 并发系列(5)之 Future 框架详解

    本文将主要讲解 J.U.C 中的 Future 框架,并分析结合源码分析其内部结构逻辑: 一.Future 框架概述 JDK 中的 Future 框架实际就是 Future 模式的实现,通常情况下我们 ...

  2. vscode下面开发vue.js项目

    vscode下面开发vue.js项目   https://blog.csdn.net/linzhiqiang0316/article/details/79176651 vscode下面开发vue.js ...

  3. [.NET] 使用 Senparc.Weixin 接入微信公众号开发:简单实现自动回复

    使用 Senparc.Weixin 接入微信公众号开发:简单实现自动回复 目录 一.前提 二.基本配置信息简析 三.配置服务器地址(URL) 四.请求处理 一.前提 先申请微信公众号的授权,找到或配置 ...

  4. int-Integer-String之间的转换方式

    1.int和Integer之间的转换:   1) int----->Integer ①自动装箱 ②Integer的构造方法 ③调用Integer的静态方法:static Integer valu ...

  5. 可达用户投资额的计算(Java)

    有话要说: 前阵子遇到了一个计算可达用户投资额的问题,觉得非常有趣,故把它记录下来. 问题描述: 某产品可被投资,注册后才可以投资,其注册可以被邀请(并不是每个人都是被邀请的).邀请人可以邀请多个人注 ...

  6. 你不可不知的Java引用类型之——Reference源码解析

    定义 Reference是所有引用类型的父类,定义了引用的公共行为和操作. reference指代引用对象本身,referent指代reference引用的对象,下文介绍会以reference,ref ...

  7. 在 vue cli3 的项目中配置双服务,模拟 ajax 分页请求

    最近安装了下vue cli3版本,与 cli 2 相比,文件少了,以前配置方法也不管用了.demo 中的大量的数据,需要做成 ajax 请求的方式来展示数据,因此,需要启动两个服务,一个用作前端请求, ...

  8. 应用shell脚本停启Tomcat

    最近在工作中频繁的操作多个tomcat,顺便就简单研究了一下 一. 简介 Shell 是一种与操作系统直接交互的程序,Unix系统中叫Bourne Shell,包括以下几种 Sh—Bourne She ...

  9. 【RL-TCPnet网络教程】第2章 嵌入式网络协议栈基础知识

    第2章        嵌入式网络协议栈基础知识 本章教程为大家介绍嵌入式网络协议栈基础知识,本章先让大家有一个全面的认识,后面章节中会为大家逐一讲解用到的协议. 基础知识整理自百度百科,wiki百科等 ...

  10. 做优化的数据库工程师请参考!CynosDB的计算层设计优化揭秘

    本文由云+社区发表 本文作者:孙旭,腾讯数据库开发工程师,9年数据库内核开发经验:熟悉数据库查询处理,并发控制,日志以及存储系统:熟悉PostgreSQL(Greenplum,PGXC等).Terad ...