Codeforces Round #415 (Div. 2)B. Summer sell-off (水题)

来源:互联网 发布:解压缩软件破解版 编辑:程序博客网 时间:2024/06/17 03:37
B. Summer sell-off
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 followingn days. For each day sales manager knows exactly, that ini-th day ki products will be put up for sale and exactlyli 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 anyf days from n next for sell-outs. On each off chosen days the number of products were put up for sale would be doubled. Thus, if oni-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 keepki 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 thei-th day and the number of clients that will come to the shop oni-th day.

Output

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

Examples
Input
4 22 13 52 31 5
Output
10
Input
4 10 20 33 50 6
Output
5
Note

In the first example we can choose days with numbers2 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 sell1 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 天可以让存货量加倍,问最大售出量为多少。


分析:一开始以为是dp,其实只需要把需求量少于存货量的直接加起来,然后再将剩下的按可增加的销量排序。

就可以得到答案。


AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct tree{long long h,w,c; //存货,需求,可增加贩卖量 }p[200000];bool com(tree a,tree b){return a.c>b.c;}int main(){long long ans=0;int n,k,t=0;scanf("%d%d",&n,&k);for(int i=0;i<n;i++){long long a,b;scanf("%lld%lld",&a,&b);if(a>=b)ans+=b;else{p[t].h=a;p[t].w=b;if(b>=2*a)p[t++].c=a;elsep[t++].c=b-a;}}sort(p,p+t,com);for(int i=0,j=0;i<t;i++,j++){if(j<k)ans+=p[i].h+p[i].c;elseans+=p[i].h;}printf("%lld\n",ans);}


原创粉丝点击