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.
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.
Print a single integer denoting the maximal number of products that shop can sell.
4 2
2 1
3 5
2 3
1 5
10
4 1
0 2
0 3
3 5
0 6
5
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代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <stdlib.h> 5 #include <iostream> 6 #include <algorithm> 7 8 using namespace std; 9 10 struct P{ 11 __int64 x; // 存货量 12 __int64 y; // 需求量 13 __int64 z; // “可增加”的销售量 14 }p[200010]; 15 bool cmp(P a,P b) 16 { 17 return a.z > b.z; 18 } 19 int main () 20 { 21 int n,f,i,j; 22 __int64 a,b; 23 while (~scanf("%d %d",&n,&f)) 24 { 25 int k = 0; 26 __int64 counts = 0; // 销售量 27 for (i = 0; i < n; i ++) 28 { 29 cin>>a>>b; 30 if (a >= b) // 当天的存货量大于等于需求量 31 counts += b; // 直接加上当天的最大销售额即可 32 else // 当天存货量小于需求量 33 { 34 p[k].x = a; 35 p[k].y = b; 36 if (a*2 <= b) 37 p[k ++].z = a; 38 else 39 p[k ++].z = b-a; 40 } 41 } 42 sort(p,p+k,cmp); 43 for (i = 0; i < k; i ++) 44 { 45 if (i < f) // 选出前f个 46 counts += (p[i].x+p[i].z); 47 else 48 counts += p[i].x; 49 } 50 cout<<counts<<endl; 51 } 52 return 0; 53 }