# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
129025 | PeppaPig | Cake 3 (JOI19_cake3) | C++14 | 2 ms | 380 KiB |
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>
#define long long long
#define pii pair<long, long>
#define x first
#define y second
using namespace std;
const int N = 2e5+5;
int n, m;
pii v[N];
long ans = -1e18;
multiset<pii> S1, S2;
int main() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%lld %lld", &v[i].x, &v[i].y);
sort(v + 1, v + n + 1, [&](const pii &a, const pii &b) {
return a.y < b.y;
});
long sum = 0;
for(int i = 1; i <= n; i++) {
S1.emplace(v[i].x, i);
S2.emplace(v[i].y, i);
sum += v[i].x;
if(S1.size() > m) {
long ret1 = sum - S1.begin()->x - 2 * (S2.rbegin()->x - S2.begin()->x);
long ret2 = sum - v[S2.begin()->y].x - 2 * (S2.rbegin()->x - next(S2.begin())->x);
if(ret1 > ret2) sum -= S1.begin()->x, S2.erase(pii(v[S1.begin()->y].y, S1.begin()->y)), S1.erase(S1.begin());
else sum -= v[S2.begin()->y].x, S1.erase(pii(v[S2.begin()->y].x, S2.begin()->y)), S2.erase(S2.begin());
}
ans = max(ans, sum - 2 * (S2.rbegin()->x - S2.begin()->x));
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |