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. CSS 如何让li横向居中显示

    先给一个简单的示例HTML代码 <body> <form id="form1" runat="server"> <div id=& ...

  2. (转) 淘淘商城系列——redis-desktop-manager的使用

    http://blog.csdn.net/yerenyuan_pku/article/details/72849791 实际工作环境中,redis会安装在服务器上,我们想使用redis服务就要使用re ...

  3. 使用Unittest做单元测试,addTest()单个case的时候却执行全部的case

    参考: http://tieba.baidu.com/p/6008699660 首先造成这个结果的原因是pycharm配置问题 问题验证: 测试代码: import unittest class Te ...

  4. js读写txt文件

    view plain<script language="javascript" type="text/javascript"> //读文件funct ...

  5. 浅谈FFC

    FFC(Flexible Formatting Context) CSS3引入了一种新的布局模型——flex布局(之前有文章介绍过).flex是flexible box的缩写,一般称之为弹性盒模型.和 ...

  6. 05Servlet example

    dgdfgdfggggggg Servlet 表单数据 在客户端,GET通过URL提交数据,数据在URL中可见:POST把数据放在form的数据体内提交.GET提交的数据最多只有1024字节:POST ...

  7. linux初步学习有感

    经过了一段时间对linux的接触,从最开始接触到的deepin到后来我最喜欢的KaliLinux,感受到了这个我曾经并不了解的操作系统的独特魅力. 我是到了大学才知道linux这个系统的,但是在小时候 ...

  8. OS X中crt中文乱码

    SecureCRT中显示乱码的话,可以去设置为UTF-8编码: Session Options->Terminal->Appearance->Character Encoding,设 ...

  9. HDU - 6158 The Designer

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6158 本题是一个计算几何题——四圆相切. 平面上的一对内切圆,半径分别为R和r.现在这一对内切圆之间,按 ...

  10. SpringBoot yaml的配置及使用

    application.yml配置如下 person:     lastName: hello     age:      boss: false     birth: //     maps: {k ...