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.

夏天到了,商店要进行促销活动。

商店在夏天中营业 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), '\0', 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=;
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=;
repd(i,,n)
{
cin>>k[i]>>l[i];
if(k[i]==)
{ }else if(k[i]>=l[i])
{
ans+=l[i];
}else
{
ans+=k[i];
a[cnt++]=min(k[i]*,l[i])-k[i];
}
}
// cout<<ans<<endl;
// db(ans);
sort(a,a+cnt);
for(int i=cnt-;i>=;i--)
{
if(f==)
{
break;
}
ans+=a[i];
// cout<<a[i]<<" == "<<endl;
f--; }
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Summer sell-off CodeForces - 810B (排序后贪心)的更多相关文章

  1. Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)

    ---恢复内容开始--- 题目: Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major ...

  2. Problem #3263 丽娃河的狼人传说 区间满足灯数,r排序后贪心。

    丽娃河的狼人传说 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: megabytes ...

  3. uva11292 Dragon of Loowater(排序后贪心)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  4. Codeforces C. Elections(贪心枚举三分)

    题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces 830A. Office Keys (贪心二分 or DP)

    原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...

  6. codeforces 480A A. Exams(贪心)

    题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Codeforces 1100F(线性基+贪心)

    题目链接 题意 给定序列,$q(1\leq q \leq 100000) $次询问,每次查询给定区间内的最大异或子集. 思路 涉及到最大异或子集肯定从线性基角度入手.将询问按右端点排序后离线处理询问, ...

  8. CodeForces - 1253C(思维+贪心)

    题意 https://vjudge.net/problem/CodeForces-1253C n个糖果,一天最多吃m个糖果,每个糖果有个值a[i],第d天会变成d*a[i],问吃k(k=1~n)个糖果 ...

  9. 将1~n个整数按字典顺序进行排序,返回排序后第m个元素

    给定一个整数n,给定一个整数m,将1~n个整数按字典顺序进行排序,返回排序后第m个元素.n最大可为5000000.字典排序的含义为:从最高位开始比较.1开头的数字排在最前面,然后是2开头的数字,然后是 ...

随机推荐

  1. MacOS远程Windows提示:远程桌面连接无法验证您希望连接的计算机的身份

    解决方法: 1.在Windows端,运行输入 “gpedit.msc”,打开本地组策略编辑器 2.依次打开[计算机配置]→[管理模板]→[windows组件]→[远程桌面服务]→[远程桌面会话主机]→ ...

  2. 解决Eclipse点击运行后控制台不能自动弹出的问题

    解决方案: 选择Window-->Preferences-->Run.Debug-->Console 勾选"Show when program writest to sta ...

  3. 编译&链接笔记

    无法解析的外部符号? 1)库的版本不对,换成X64或Win32试试

  4. 17.基于scrapy-redis两种形式的分布式爬虫

    redis分布式部署 1.scrapy框架是否可以自己实现分布式? - 不可以.原因有二. 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls ...

  5. spark SQL读取ORC文件从Driver启动到开始执行Task(或stage)间隔时间太长(计算Partition时间太长)且产出orc单个文件中stripe个数太多问题解决方案

    1.背景: 控制上游文件个数每天7000个,每个文件大小小于256M,50亿条+,orc格式.查看每个文件的stripe个数,500个左右,查询命令:hdfs fsck viewfs://hadoop ...

  6. DP h回文子串 LCS

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

  7. C#多线程の遇见长耗时操作以及多任务(简明记要)

    4.0用         Task.Factory.StartNew(()=>{});4.0以下用  ThreadPool.QueueUserWorkItem(()=>{})4.0以上用 ...

  8. centos7下安装docker(17.3docker监控---cAdvisor)

    cAdvisor是google开发的容器监控工具 1.在host上运行cadvisor容器 docker run -d -p 8080:8080 --name cadvisor -v /:/rootf ...

  9. Python:Day14 集合、函数

    浅copy只copy一层 深copy相当于克隆一份 深copy要引入copy,具体如下: import copy wife = copy.copy() #此为浅copy,括号中要加copy的对象,相当 ...

  10. BuildTools Overview

    SCons Pros: Based on a full-fledged programming language, Python. This means you can make the build ...