CF-(贪心)

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.

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


/*
题意:
一共n天,
每天有ki件物品,你知道每天能卖掉li件; 然后让你选f天;
这f天,可以将ki乘上2; 每天结束之后物品清空; 问你最多能卖多少件物品?

题解:
按照每件物品被选中后的增值从大到小排序,最后遍历一遍就可以了

*/
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct node
{
	long long int sum, cost;
}s[100005];

bool cmp(node ss, node tt)
{
	long long int  tmp1 = ss.sum * 2;
	long long int  tmp2 = tt.sum * 2;
	long long int ans1, ans2;
	tmp1 >= ss.cost ? ans1 = ss.cost : ans1 = tmp1;
	tmp2 >= tt.cost ? ans2 = tt.cost : ans2 = tmp2;
	long long int ans3, ans4;
	ss.sum >= ss.cost ? ans3 = ss.cost : ans3 = ss.sum;
	tt.sum >= tt.cost ? ans4 = tt.cost : ans4 = tt.sum;
	ans1 -= ans3;
	ans2 -= ans4;

	return ans1 > ans2;
}

int main()
{
	int n, f;
	while (cin >> n >> f)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%lld%lld", &s[i].sum, &s[i].cost);
		}
		sort(s, s + n, cmp);
		long long int ans = 0;

		for (int i = 0; i < n; i++)
		{
			if (s[i].sum == 0 || s[i].cost == 0)
				continue;
			if (f)
			{
				if (s[i].sum * 2 >= s[i].cost)
				{
					ans += s[i].cost;
				}
				else
				{
					ans += s[i].sum * 2;
				}
				f--;
			}
			else
			{
				if (s[i].cost >= s[i].sum)
				{
					ans += s[i].sum;
				}
				else
				{
					ans += s[i].cost;
				}
			}
		}

		printf("%lld\n", ans);
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值