#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX = 2e5 + 5;
int N, K;
struct cake{
ll V, C;
cake() : V(0), C(0) {}
cake(int V, int C) : V(V), C(C) {}
bool operator < (const cake& o){
return make_pair(C, -V) < make_pair(o.C, -o.V);
}
friend istream& operator >> (istream& in, cake& c){
return in >> c.V >> c.C;
}
} cakes[MAX];
struct F{
int keep;
ll sum;
priority_queue<ll, vector<ll>, greater<ll>> pq;
F(int k) : pq(), keep(k), sum(0) {}
void insert(ll x){
if((int)pq.size() < keep){
sum += x;
pq.push(x);
} else{
ll mn = pq.top();
if(mn < x){
pq.pop();
sum -= mn;
sum += x;
pq.push(x);
}
}
}
ll get(){
return sum;
}
void reset(){
sum = 0;
priority_queue<ll, vector<ll>, greater<ll>>().swap(pq);
}
};
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
cin >> N >> K;
for(int i = 1; i <= N; ++i){
cin >> cakes[i];
}
sort(cakes + 1, cakes + 1 + N);
for(int i = 2; i <= N; ++i){
assert(cakes[i - 1].C <= cakes[i].C);
}
ll ans = 0;
F max_sums(K);
for(int i = 1; i + K - 1 <= N; ++i){
max_sums.reset();
for(int j = i; j <= i + K - 2; ++j){
max_sums.insert(cakes[j].V);
}
for(int j = i + K - 1; j <= N; ++j){
max_sums.insert(cakes[j].V);
ll earn = max_sums.get();
ll cost = 2LL * (cakes[j].C - cakes[i].C);
ans = max(ans, earn - cost);
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |