B. Summer sell-off
time limit per test  

1 second

memory limit per test  

256 megabytes

 

Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.

Shop, where Noora is working, has a plan on the following n days. For each day sales manager knows exactly, that in i-th day kiproducts will be put up for sale and exactly li clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump.

For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any f days from n next for sell-outs. On each of f chosen days the number of products were put up for sale would be doubled. Thus, if on i-th day shop planned to put up for sale ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·ki products. Consequently, there is an opportunity to sell two times more products on days of sell-out.

Noora's task is to choose f days to maximize total number of sold products. She asks you to help her with such a difficult problem.

Input

The first line contains two integers n and f (1 ≤ n ≤ 105, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out.

Each line of the following n subsequent lines contains two integers ki, li (0 ≤ ki, li ≤ 109) denoting the number of products on the shelves of the shop on the i-th day and the number of clients that will come to the shop on i-th day.

Output

Print a single integer denoting the maximal number of products that shop can sell.

 
input
4 2
2 1
3 5
2 3
1 5
output
10
input
4 1
0 2
0 3
3 5
0 6
output
5
Note:

In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total1 + 5 + 2 + 2 = 10 product units.

In the second example it is possible to sell 5 products, if you choose third day for sell-out.

题目大意:

     有一个超市,现已知接下来n天每天的存货量和需求量,其中可以选f天使得当天的存货量翻倍,问这n天的最大销售量可以是多少?

解题思路:

     这题可以分两种情况:

     1  当天的存货量大于等于需求量的时候,这一天的存货量是不需要翻倍的

     2  当天存货量小于需求量的时候,先让这一天的存货量翻倍,求出当天 ”可增加“ 的销售量

     然后根据可增加的销售量从大到小排序,选出前f天。

AC代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> using namespace std; struct P{
__int64 x; // 存货量
__int64 y; // 需求量
__int64 z; // “可增加”的销售量
}p[];
bool cmp(P a,P b)
{
return a.z > b.z;
}
int main ()
{
int n,f,i,j;
__int64 a,b;
while (~scanf("%d %d",&n,&f))
{
int k = ;
__int64 counts = ; // 销售量
for (i = ; i < n; i ++)
{
cin>>a>>b;
if (a >= b) // 当天的存货量大于等于需求量
counts += b; // 直接加上当天的最大销售额即可
else // 当天存货量小于需求量
{
p[k].x = a;
p[k].y = b;
if (a* <= b)
p[k ++].z = a;
else
p[k ++].z = b-a;
}
}
sort(p,p+k,cmp);
for (i = ; i < k; i ++)
{
if (i < f) // 选出前f个
counts += (p[i].x+p[i].z);
else
counts += p[i].x;
}
cout<<counts<<endl;
}
return ;
}

Codeforces Round #415 (Div. 2) B. Summer sell-off的更多相关文章

  1. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

    A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  2. Codeforces Round#415 Div.2

    A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...

  3. Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律

    A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. Codeforces Round #415 (Div. 2) 翻车啦

    A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. Codeforces Round #415(Div. 2)-810A.。。。 810B.。。。 810C.。。。不会

    CodeForces - 810A A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes i ...

  6. Codeforces Round #415 Div. 1

    A:考虑每对最大值最小值的贡献即可. #include<iostream> #include<cstdio> #include<cmath> #include< ...

  7. Codeforces Round #415 (Div. 2)C

    反正又是一个半小时没做出来... 先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况) 又有a[ ...

  8. Codeforces Round #415 (Div. 2) C. Do you want a date?

    C. Do you want a date?   2 seconds 256 megabytes   Leha decided to move to a quiet town Vičkopolis, ...

  9. Codeforces Round #415 (Div. 1) (CDE)

    1. CF 809C Find a car 大意: 给定一个$1e9\times 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和. 可以发现$ ...

随机推荐

  1. Mysql:如果数据存在则更新,不存在则插入

    mysql语法支持如果数据存在则更新,不存在则插入,首先判断数据存在还是不存在的那个字段要设置成unique索引, 例如表tb_addrbook如下: 索引: 语句1:不存在插入 INSERT INT ...

  2. springAOP实现方法运行时间统计

    aop的before和after,寻思分别在两个方法里获得当前时间,最后一减就可以了. 因此,今天就探讨了一下这个问题,和大家分享一下. 创建maven工程,引入spring的依赖包,配置好appli ...

  3. rest-assured之认证授权(Authentication)

    rest-assured支持多种认证授权方案,比如:OAuth.digest(摘要认证).certificate(证书认证).form(表单认证)以及preemptive(抢占式基础认证)等.我们可以 ...

  4. [LnOI2019]加特林轮盘赌

    Luogu5249 轮流开枪打一个环上的人 , 每次\(p\)的概率打死 , \(p\)始终相同 , 从第\(1\)个人开始 , 求第\(k\)个人成为唯一幸存者的概率 \(19.3.30\) 官方题 ...

  5. JAVA数据结构--哈希表的实现(分离链接法)

    哈希表(散列)的定义 散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度 ...

  6. mysql里面 limit的奇效

    项目里面有遇到一个需求,查询一个表,先group by ,再按group 的count(*)进行倒序,取出每个group里面发表时间最新的一个纪录,之前的同事SQL是这样写的 SELECT * FRO ...

  7. apache2 + django 路径问题

    问题: 在代码中使用sys.path.append(), 添加模块路径后,仍然报错找不到包. 虽然在LD_LIBRARY_PATH中配置了.so文件打路径,仍然报错找不到. 原因: 检查apahce2 ...

  8. kafka集群安装及简单使用

    关于kafka是什么及原理,请参考kafka官方文档的介绍:http://kafka.apache.org/documentation/#introduction ,英文不好的同学可以看这里http: ...

  9. Python机器学习库sklearn的安装

    Python机器学习库sklearn的安装 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上能够为用户提供各种机器学习算法接口 ...

  10. 软工网络15Alpha阶段敏捷冲刺博客汇总

    博客链接汇总: 第一篇:http://www.cnblogs.com/pubg722/p/8891605.html 第二篇:http://www.cnblogs.com/pubg722/p/89090 ...