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. PostMan --API调试工具

    https://blog.csdn.net/fxbin123/article/details/80428216

  2. [BZOJ 5158][Tjoi2014]Alice and Bob

    传送门 \(\color{green}{solution}\) 贪心 /************************************************************** P ...

  3. windbg调试驱动程序

    不正确之处欢迎指正,高手勿喷~ 配置windbg路径 Symbol path:SRV*F:\Windows\symbolxp3*http://msdl.microsoft.com/download/s ...

  4. mysql统计字段中某一字符串出现的次数

    (LENGTH(t.`range_00`) - LENGTH(REPLACE (t.`range_00`, "false", ""))) / LENGTH(&q ...

  5. [Java基础]-- Java GC 垃圾回收器的分类和优缺点

    https://blog.csdn.net/high2011/article/details/80177473?utm_source=blogxgwz2 参考:elasticsearch实战-使用G1 ...

  6. java实现猴子选大王问题(约瑟夫问题)

    题目:m只猴子围成一圈报数,报n的猴子自动离开,然后下一位重新从1开始报数,一直循环,最后剩下的那个猴子就是猴大王,写出程序求出最后是大王的那只猴子最初的位置. package learn; impo ...

  7. ORACLE迁移GP实践

    最近在做oracle到greenplum的迁移实践,步骤如下: 1. 使用ora2pg实现Oracle的数据结构迁移到GP的实现过程 2. Oracle的数据迁移到GP的实现过程   1. ora2p ...

  8. Hibernate实体类编写规则和主键策略

    一.实体类的编写规则 1.属性要是私有的. 2.要有公开的setter和getter方法供外界访问和修改. 3.每一个实体类要有一个属性作为唯一值(一般都是使用对于数据表的主键). 4.建议数据类型不 ...

  9. React项目的打包

    1.create-react-app 来自Facebook官方的零配置命令行工具.create-react-app是来自于Facebook出品的零配置命令行工具,能够帮你自动创建基于Webpack+E ...

  10. fetch将替代ajax?

    原谅我做一次标题党,Ajax 不会死,传统 Ajax 指的是 XMLHttpRequest(XHR),未来现在已被 Fetch 替代. 最近把阿里一个千万级 PV 的数据产品全部由 jQuery 的  ...