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
4 2
2 1
3 5
2 3
1 5
10
4 1
0 2
0 3
3 5
0 6
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 天。每天出售的商品数已经确定,第 i 天出售 ki 件商品,有 li 个客户回来购买。已知每个客户恰好购买一件商品。如果没有商品可以购买,那客户就会选择空手离开。
由于要进行促销,你可以选择 f 天将出售的商品数翻倍,但是客户的数量不会翻倍。现在让你求出最多能卖出多少件商品。
思路:先把每一天的不翻倍能卖出多少个商品加入到ans中,然后再开一个数组(其实用大根堆最方便)来记录每天如果这一天翻倍的话,能多卖出多少商品。
然后把这个新数组排序,然后取前f个加入到ans中。
细节见我的AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), ' ', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== "<<x<<" =="<<endl; using namespace std; typedef long long ll; inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n,f; int k[maxn]; int l[maxn]; int a[maxn]; // priori int main() { gbtb; cin>>n>>f; ll ans=0ll; int cnt=0; repd(i,1,n) { cin>>k[i]>>l[i]; if(k[i]==0) { }else if(k[i]>=l[i]) { ans+=l[i]; }else { ans+=k[i]; a[cnt++]=min(k[i]*2,l[i])-k[i]; } } // cout<<ans<<endl; // db(ans); sort(a,a+cnt); for(int i=cnt-1;i>=0;i--) { if(f==0) { break; } ans+=a[i]; // cout<<a[i]<<" == "<<endl; f--; } cout<<ans<<endl; return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == ' '); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }