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. python 3 map函数用法

    公式 f是定义的函数,l是你的list,所有功能都在f函数里完成, map(f,l) 有些网址爬虫出来的链接是一部分,省略了前端通用的,这时我们需要补充进去, 这时就用到了map函数,批量补充网址, ...

  2. 仿照swpu邮寄系统的登录页面

    实验过程 跟着老师的文档过了一遍手,稍作了修改 效果展示 页面在网盘: 链接:https://pan.baidu.com/s/1jsT0SDiiJXzPtR93ZAh1YA 提取码:9miq

  3. C#的datatable使用

    // 构造datatable DataTable dt = new DataTable("test_table"); dt.Columns.AddRange(new DataCol ...

  4. js中报错"Maximum call stack size exceeded"解决方法

    Uncaught RangeError: Maximum call stack size exceeded 错误直译过来就是“栈溢出”,出现这个错误的原因是因为我进行了递归运算,但是忘记添加判断条件, ...

  5. 使用maven构建多模块项目_记录

    参照孤傲苍狼的博客:https://www.cnblogs.com/xdp-gacl/p/4242221.html 备注:博客中的生成语句,适用于maven3.0.5以上,若为3.0.5以下,则将cr ...

  6. JS及Dom练习 | 页面滚动文字

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【Css】一个简单的图片库

    今天做一个简单的图片库! 其实这个在w3school教程里介绍得很好了,不过看到什么,自己动手做一次,记得也深刻不是. 我们分几步来走: 第一步:先写一个坯子. <html> <he ...

  8. 批量修改dos文件到unix

    1. 安装dos2unix 2. 执行:find ./ -type f | xargs dos2unix

  9. 架构实战项目心得(九):后台服务工具ldap:统一用户中心ldap工具使用以及安装

    一.安装OpenLDAP 1.安装 yum -y install openldapopenldap-servers openldap-clients openldap-devel compat-ope ...

  10. [转]前端构建工具gulpjs的使用介绍及技巧

    本文转自:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非 ...