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):基于OpenCV实现小型的图像数据库检索

    本文对前面的几篇文章进行个总结,实现一个小型的图像检索应用. 一个小型的图像检索应用可以分为两部分: train,构建图像集的特征数据库. retrieval,检索,给定图像,从图像库中返回最类似的图 ...

  2. 基于CentOS7系统部署cobbler批量安装系统(week3_day5_part1)-技术流ken

    前言 cobbler是一个可以实现批量安装系统的Linux应用程序.它有别于pxe+kickstart,cobbler可以实现同个服务器批量安装不同操作系统版本. 系统环境准备及其下载cobbler ...

  3. function string类型的参数传递

    1.错误案例: Index:1 Uncaught ReferenceError: 系统管理 is not defined at HTMLAnchorElement.onclick (Index:1) ...

  4. c#实战开发:以太坊钱包快速同步区块和钱包卡死解决方案 (三)

    首先以太坊默认的快速同步模式 我们需要先设置当前同步模式内存大小512-2048范围 在服务器配置情况下最大化内存 输入以下命令 geth --fast --cache=2048 最快同步模式也是 保 ...

  5. phpcms V9 二次开发------(获取点击数详解)

    关于phpcms V9的点击数的使用应该有不少数是直接调用网上搜索到的代码,但是对于一些想要深入研究开发的人来说,看到网上的代码后更是不解,本人这几天看了看,了解了一些东西,在这里写出来分享一下,首先 ...

  6. Ionic3关闭弹出页面,跳转到列表后刷新父页面

    记得上次写过一篇如何弹出页面的文章,好像是2月28号ionic3 Modal组件那一篇,这篇也算那一篇的续集吧!这篇是弹出的页面关闭后刷新父页面的干活!上代码! 弹出页面:(关闭的时候可以传入值,再父 ...

  7. SpringBoot+MyBatis配置多数据源

    SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...

  8. matplotlib箱线图与柱状图比较

    代码: # -*- coding: utf-8 -*- """ Created on Thu Jul 12 16:37:47 2018 @author: zhen &qu ...

  9. EOS开发环境搭建

    EOS开发环境搭建   在上一篇文章<扒一扒EOS的前世今生>中,我们已经了解了EOS以及他的创始人Daniel Larimer的故事,本次为大家带来的是关于EOS开发环境搭建的内容.首先 ...

  10. .net core 命令行(仅作记录)

    命令大全:dotnet 命令 创建NuGet包:如何使用 .NET Core 命令行接口 (CLI) 工具创建 NuGet 包