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. Yii2.0手册地址

    官网打不开,可以看这里 http://yii2.techbrood.com/   ;跟官网里面文档一样.ps:今天真郁闷,官网都打不开

  2. Docker: dockerfile常用关键字

    Dockerfile指令 Dockfile执行和shell命令一行,一行一行执行- 写Dockerfile注意点: 1.           尽量少RUN 2.           多个命令拼接在一起 ...

  3. Python Numpy-基础教程

    目录 1. 为什么要学习numpy? 2. Numpy基本用法 2.1. 创建np.ndarry 2.2. Indexing and Slicing Boolean Index 2.3. Univer ...

  4. 关于Three.js基本几何形状

    一.有关球体SphereGeometry构造函数参数说明 SphereGeometry(radius, widthSegments, heightSegments, phiStart, phiLeng ...

  5. Ubuntu 普通用户无法启动Google chrome

    删除 /home/你的用户名/.config/google-chrome文件,再打开就好了 root@ecos:cd /home/ecos/.config root@ecos:~/.config# r ...

  6. 【大数据技术】HBase介绍

    1.HBase简介1.1 Hbase是什么HBase是一种构建在HDFS之上的分布式.面向列.多版本.非关系型的数据库,是Google Bigtable 的开源实现. 在需要实时读写.随机访问超大规模 ...

  7. cookie 处理 以及模拟登陆

    cookie的处理 1.手动处理: cookie封装到headers 2.自动处理: 1.获取一个session对象 2.使用session对象进行请求的发送 3.作用:在使用session进行请求发 ...

  8. Python 属性与方法 概念理解

    属性与方法 attribute(属性)是class(类)中的成员变量,而method(方法)则是class(类)中的function(函数). 也可以理解,属性就类变量,方法就是类函数. 类中的变量就 ...

  9. visual studio发布到远程服务器的IIS

    visual studio发布到远程服务器的IIS 1.打开项目,选中发布的项目. 2.右键发布的项目,点击菜单中的“发布”,弹出发布配置窗体,如下图 3.选择“自定义”,如下图 4.填写发布配置的名 ...

  10. 【转】如何修改 video 样式

    我们这里说的“修改 video 样式”并不是要自己实现一套 controls,而是尝试修改 video 的默认样式 隐藏全屏按钮 这个很容易查到 video::-webkit-media-contro ...