This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 2e5 + 5;
int n, m;
int cl, cr;
long long best, ans;
pair<int, int> a[N];
multiset<int> small, big;
void ins(int x) {
if ((int) big.size() < m) {
best += x;
big.insert(x);
return;
}
int y = *big.begin();
if (y < x) {
small.insert(y);
big.erase(big.find(y));
best -= y;
big.insert(x);
best += x;
} else {
small.insert(x);
}
}
void erase(int x) {
if (!small.empty() && *small.rbegin() >= x) {
small.erase(small.find(x));
return;
}
best -= x;
big.erase(big.find(x));
if (!small.empty()) {
int y = *small.rbegin();
small.erase(small.find(y));
big.insert(y);
best += y;
}
}
void edit(int l, int r) {
while (cl > l) ins(a[--cl].second);
while (cr < r) ins(a[++cr].second);
while (cl < l) erase(a[cl++].second);;
while (cr > r) erase(a[cr--].second);
}
void solve(int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) / 2;
pair<long long, int> opt = {-LLONG_MAX, -1};
for (int i = max(mid, optl); i <= optr; ++i) {
edit(mid, i);
long long val = best - 2 * (a[i].first - a[mid].first);
if (i - mid + 1 >= m) {
opt = max(opt, make_pair(val, i));
}
}
assert(opt.second != -1);
ans = max(ans, opt.first);
solve(l, mid - 1, optl, opt.second);
solve(mid + 1, r, opt.second, optr);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i].second >> a[i].first;
}
sort(a + 1, a + 1 + n);
cl = 1, cr = 0;
solve(1, n, 1, n);
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... |