题解 P2707 【Facer帮父亲】
Just_do_it · · 题解
思路:
我们将收益表达出来是
当然没有函数基础的人可以这么想
当前收益
我们发现
且
这个改变量与
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline void read(int &x){
int f = 1;x = 0;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x*10+ch-'0';ch = getchar();}
x *= f;
}
const int N = 100005;
int n,k;
ll ans;
struct node{
int val,b;
friend bool operator <(node a,node b){
return a.val < b.val;
}
}u;
priority_queue<node> Q;
int main(){
read(n); read(k);
for(int i = 1,a,b;i <= n;++i){
read(a),read(b);
if(a-b > 0) Q.push((node){a-b,b});
}
while((k--) && (!Q.empty())){
u = Q.top(); Q.pop();
ans += u.val; u.val -= 2*u.b;
if(u.val <= 0) continue;
Q.push(u);
}
printf("%lld\n",ans);
return 0;
}