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 mcontainers, 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.

Sample test(s)
input
7 3
5 10
2 5
3 6
output
62
input
3 3
1 3
2 2
3 1
output
7

非常水的贪心  直接每次选装的多的盒子  n减去盒子数即可了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 25;
int a[M], b[M], c[M], n, m, ans; bool cmp (int i, int j)
{
return b[i] > b[j];
} int main()
{
scanf ("%d%d", &n, &m);
for (int i = 1; i <= m; ++i)
{
c[i] = i;
scanf ("%d%d", &a[i], &b[i]);
}
sort (c + 1, c + m + 1, cmp);
ans = 0;
for (int i = 1; i <= m; ++i)
{
if (n >= a[c[i]])
{
n -= a[c[i]];
ans += a[c[i]] * b[c[i]];
}
else
{
ans += n * b[c[i]];
break;
}
}
printf ("%d\n", ans);
return 0;
}

CodeForces 16B Burglar and Matches(贪心)的更多相关文章

  1. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  2. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  3. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  4. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  5. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  6. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

  7. Codeforces 980E The Number Games 贪心 倍增表

    原文链接https://www.cnblogs.com/zhouzhendong/p/9074226.html 题目传送门 - Codeforces 980E 题意 $\rm Codeforces$ ...

  8. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  9. Codeforces 402D Upgrading Array:贪心 + 数学

    题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...

随机推荐

  1. day21-1 类的继承

    目录 类的继承 什么是继承 为什么用继承 对象的继承 继承与抽象 继承的应用 对象属性查找顺序 类的继承 什么是继承 继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承) ...

  2. CAD设置水印

    主要用到函数说明: _DMxDrawX::Watermark 设置控件水印图片显示,字符串用逗号隔开,分为: “文件名,透明度,x方向距离,y方向距离,是否居中”, 是否居中0代表在上角定位,1表示自 ...

  3. java学习_5_21

    数组的插入.删除.扩容本质上都是用的数组的复制.Java中数组的拷贝如下: System.arraycopy(Object src, int srcPos, Object dest, int dest ...

  4. JavaScipt30(第三个案例)(主要知识点:css变量)

    承接上文 https://www.cnblogs.com/wangxi01/p/10641210.html,下面是第三个案例: 附上项目链接: https://github.com/wesbos/Ja ...

  5. 【Hadoop】三、HDFS命令行接口

      通过前面对HDFS基本概念.高可用性.数据读写流程的介绍,我们对HDFS已经有了大致的了解.这里我们还需要明确一点:Hadoop作为一个完整的分布式系统,它有一个抽象的文件系统的概念,而我们介绍的 ...

  6. 关于fragment+viewpager的优化

    上次写了一个问答项目,用的fragment+viewpager架构,后来发现,划了几次之后,再划回来,会重新加载布局,重新获取数据,这样整个程序和卡,并且占用太多的网络资源. 当时的解决办法是,自己重 ...

  7. 简单的jsonp实现跨域原理

    什么原因使jsonp诞生?  传说,浏览器有一个很重要的安全限制,叫做"同源策略".同源是指,域名,协议,端口相同.举个例子,用一个浏览器分别打开了百度和谷歌页面,百度页面在执行脚 ...

  8. 洛谷——P1404 平均数

    P1404 平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 前缀和+二分答案 #include<iostream> ...

  9. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  10. jupyter notebook的插件安装及文本格式修改

    jupyter notebook的插件安装及文本格式修改 1.jupyter notebook拓展插件安装 启动jupyter notebook : 打开控制台输入命令 jupyter noteboo ...