CodeForces #415(div2) Summer sell-off(贪心)

来源:互联网 发布:淘宝抠图兼职怎么找 编辑:程序博客网 时间:2024/05/24 08:32

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


一开始想这个题的时候按照一个错误的标准贪心。以为按照min(k*2,l)这样从大到小排一下取前n个进行加倍后面的都按照min(k,l)取就行了。但是这样如果本身l就小于k加倍和不加倍所得到的是一样的,就浪费了加倍的机会。

于是就有下面的思路。

思路:先按照加倍后商品能多卖出去的件数more来排序,如果more相等那么就按照能卖出去多的排序,因为存在more都为0,但是卖出的件数是不相同的可能。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX_N 100500using namespace std;struct node{    int k,l,more,sell;}q[MAX_N];bool cmp(node a,node b){    if(a.more!=b.more)        return a.more>b.more;    return a.sell>b.sell;}int main(){    int f,n;    while(~scanf("%d%d",&f,&n))    {        for(int i=0;i<f;i++)        {            scanf("%d%d",&q[i].k,&q[i].l);            if(q[i].k<q[i].l)            {                if(q[i].k*2>q[i].l)                    q[i].more=q[i].l-q[i].k;                else                    q[i].more=q[i].k;            }            else                q[i].more=0;            q[i].sell=min(q[i].k*2,q[i].l);        }        sort(q,q+f,cmp);        long long ans=0;        for(int i=0;i<n;i++)            ans+=q[i].sell;        for(int i=n;i<f;i++)            ans+=min(q[i].l,q[i].k);        cout<<ans<<endl;    }    return 0;}