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
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.
题目来源
http://codeforces.com/problemset/problem/810/B
题目分析:
题目的意思是给你n组数据,第一个表示商品数,第二个表示顾客数。每天最多可以卖出k件商品,k取决于顾客数和商品数的最小值。然后你还可以选择F天,将卖商品数目翻倍。最后输入能卖出商品的最大值。
题目思路:
定义一个结构体 shop 里面记录正常情况下能卖出的商品数value[0],商品翻倍下能卖出的商品数value[1],和翻倍后能创造的价值c。再按照价值c进行排序,将前F个商品都翻倍,其余的正常相加。
题目小坑:
数据较大注意long long !
数据较多,如果冒泡排序的话会 TLE!!!
小编比较傻*…前面一直卡在long long的数据,后面改了之后卡在了冒泡排序…卡着卡着时间就过了。QAQ只能补题了 哎~以后要多多注意啊。
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct shop
{
long long value[2],c;
};
bool cmp(shop v1,shop v2)
{
return v1.c>v2.c;
}
int main()
{
int n,f;
while(scanf("%d %d",&n,&f)!=EOF)
{
long long a,b;
shop v[100000];
for(int i=0;i<n;i++)
{
scanf("%lld %lld",&a,&b);
v[i].value[0] = a>b?b:a;
v[i].value[1] = (a*2)>b?b:(a*2);
v[i].c = v[i].value[1] - v[i].value[0];
}
sort(v,v+n,cmp);
long long sum = 0;
for(int i=0,j=0;i<n;i++,j++)
{
if(j<f)
{
sum += v[i].value[1];
}
else
sum += v[i].value[0];
}
printf("%lld\n",sum);
}
return 0;
}