time limit per test
1 second
memory limit per test

256 megabytes

input

standard input

output

standard output

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 ki products 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.

Examples
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 total 1 + 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天让商品数量*2使得最终卖出最多的东西。

举个栗子:

4 2

2 1
3 5
2 3
1 5

4天里面选2天

第一竖行是商品数量,第二竖行是顾客数量(不用管题目中的保质期(英语不好,被这个保质期搞得有点傻(/ω\)))

第一个数据,2个商品,1个顾客,不管*2还是不*2,商品数量都比顾客数量多,没用

第二个数据,3个商品,5个顾客,3<5,*2之后是6个商品,最后卖出去5个(因为就5个顾客)

所以,就先把没*2的时候可以卖出去的数量先存到一个数组里,然后求和sum。

然后再每一个数据都*2,把还可以继续卖出去的数量存到另一个数组里,然后排排坐,大的在前面,小的在后面(保证是卖出去最多的)

之后按照要求,因为是其中f天的商品数量*2,所以把排好序的另一个数组里的前f个加到sum里,就可以了

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct node{
int ss;
int cc;
}a[N];
bool cmp(int a,int b){
return a>b;
}
int main(){
int b[N],c[N];
int n,m;
ll sum;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].ss,&a[i].cc);
for(int i=0;i<n;i++){
if(a[i].ss>=a[i].cc){
b[i]=a[i].cc;
c[i]=0;
}
else if((a[i].cc>a[i].ss)&&(2*a[i].ss>a[i].cc)){ //这里一开始写错了2*a[i].ss>a[i].cc写成<了
b[i]=a[i].ss;
c[i]=a[i].cc-a[i].ss;
}
else if(2*a[i].ss<=a[i].cc){
b[i]=a[i].ss;
c[i]=a[i].ss;
}
}
sort(c,c+n,cmp);
sum=0;
for(int i=0;i<n;i++)
sum+=b[i];
for(int i=0;i<m;i++)
sum+=c[i];
printf("%lld\n",sum);
}
return 0;
}

Codeforces 810 B. Summer sell-off的更多相关文章

  1. codeforces 810 D. Glad to see you!(二分+互动的输入方式)

    题目链接:http://codeforces.com/contest/810/problem/D 题意:两个人玩一场游戏要猜出Noora选的f种菜的任意两种.一个人猜点另一个人回答 TAK如果 ,(x ...

  2. CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)

    https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...

  3. Codeforces 810 C. Do you want a date?

    C. Do you want a date? time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. Codeforces 810 A.Straight «A»

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

  5. CF 810 D. Glad to see you!

    codeforces 810 D. Glad to see you! http://codeforces.com/contest/810/problem/D 题意 大小为k的集合,元素的范围都在[1, ...

  6. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  7. Codeforces Round #437 E. Buy Low Sell High

    题意:买卖股票,给你n个数,你可以选择买进或者卖出或者什么都不做,问你最后获得的最大收益是多少. Examples Input 910 5 4 7 9 12 6 2 10 Output 20 Inpu ...

  8. Buy Low Sell High CodeForces - 867E (思维,贪心)

    大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...

  9. 【CodeForces】866D. Buy Low Sell High

    [题意]已知n天股价,每天可以买入一股或卖出一股或不作为,最后必须持0股,求最大收益. [算法]堆 贪心? [题解] 不作为思想:[不作为=买入再卖出] 根据不作为思想,可以推出中转站思想. 中转站思 ...

随机推荐

  1. [剑指Offer] 23.二叉搜索树的后序遍历

    [思路]BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两 ...

  2. SQL Server Profiler的简单使用,方便查找和发现SQL执行的效率和语句问题

    1 打开Server Profiler 2 去掉不必要的干扰,数据库的连接和断开之类的 3. 选择“显示所有列”,之后在列表中,勾选“DatabaseName”项. 4设置筛选器,这里设置只是过滤数据 ...

  3. AJAX基本演示使用

    Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...

  4. Codeforces Round #518 Div. 1没翻车记

    A:设f[i][j][0/1]为前i个数第i位为j且第i位未满足/已满足限制的方案数.大力dp前缀和优化即可. #include<iostream> #include<cstdio& ...

  5. Codeforces Round #392 (div.2) E:Broken Tree

    orz一开始想不画图做这个题(然后脑袋就炸了,思维能力有待提高) 我的做法是动态规划+贪心+构造 首先把题目给的树变成一个可行的情况,同时weight最小 这个可以通过动态规划解决 dp[x]表示以x ...

  6. 【题解】Bzoj4316小C的独立集

    决定要开始学习圆方树 & 仙人掌相关姿势.加油~~ 其实感觉仙人掌本质上还是一棵树,长得也还挺优美的.很多的想法都可以往树的方面上靠,再针对仙人掌的特性做出改进.这题首先如果是在树上的话那么实 ...

  7. 【题解】NOIP2016换教室

    哇好开心啊!写的时候真的全然对于这个加法没有把握,但还是大着胆子试着写了一下——竟然过了样例?于是又调了一下就过啦. 不过想想也觉得是正确的吧,互相独立的事件对于期望的影响自然也是相互独立的,可以把所 ...

  8. HDU4289:Control(最小割)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. ionic改tab文字和icon图片的颜色

    The official way would be: Change in your theme/variables.scss Active icon for tabs on android is: $ ...

  10. poj 2104 (主席树写法)

    //求第K的的值 1 #include<stdio.h> #include<iostream> #include<algorithm> #include<cs ...