codeforces 810B

来源:互联网 发布:淘宝白菜价在哪里 编辑:程序博客网 时间:2024/06/05 04:04
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 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

题意:

         有n天,每天都有ki件物品,而且每天都能卖出去li件物品,如果有剩下的物品则不会留在下一天,你可以选择f天,被选中的那一天ki可以乘以二,问n天后最多能卖出去多少物品

思路:

         贪心,将没有乘以二是将n天可以卖出去的总数求出,然后求出每一天ki*2后能多卖出去多少,然后排序选择前f项

注意:

         小心数据溢出

#include<iostream>#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 1e5+5;struct Node{    int k;    int l;}node[N];int b[N];bool cmp(int x,int y){    return x>y;}int main(){    cin.sync_with_stdio(false);    int n,f;    while(cin>>n>>f)    {        ll sum=0;        int k,l;        memset(b,0,sizeof(b));        for(int i=0;i<n;i++)        {            cin>>node[i].k>>node[i].l;            sum+=min(node[i].k,node[i].l);        }        for(int i=0;i<n;i++)        {            if(node[i].k<node[i].l)            {                if(node[i].k*2>node[i].l)                    b[i]=node[i].l-node[i].k;                else                    b[i]=node[i].k;            }        }        sort(b,b+n,cmp);        for(int i=0;i<f;i++)            sum+=b[i];        cout<<sum<<endl;    }    return 0;}