# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
362673 | hoaphat1 | Cake 3 (JOI19_cake3) | C++17 | 4042 ms | 10808 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>
using namespace std;
int main() {
#define qwer "test"
if (fopen(qwer".inp","r")) freopen(qwer".inp","r",stdin),freopen(qwer".out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) cin >> a[i].second >> a[i].first;
sort(a.begin(), a.end());
/*
k1 k2 ... km -> v - 2 * (c km - c k1)
i >= i-1
*/
long long ans = -1e18;
multiset<int> pq;
multiset<int> smaller;
long long now = 0;
int L = 0, R = -1;
auto add = [&](int v) {
pq.insert(v);
now += v;
if (pq.size() > m - 2) {
smaller.insert(*pq.begin());
now -= *pq.begin();
pq.erase(pq.begin());
}
};
auto del = [&](int v) {
auto it = pq.find(v);
if (it != pq.end()) {
now -= *it;
pq.erase(it);
if (!smaller.empty()) {
pq.insert(*prev(smaller.end()));
now += *prev(smaller.end());
smaller.erase(prev(smaller.end()));
}
}
else {
smaller.erase(smaller.find(v));
}
};
auto moveL = [&](int l) {
while (L < l) del(a[L++].second);
while (L > l) add(a[--L].second);
};
auto moveR = [&](int r) {
while (R < r) add(a[++R].second);
while (R > r) del(a[R--].second);
};
function<void(int, int, int, int)> solve = [&](int l, int r, int L, int R) {
if (l > r) return ;
int mid = l + r >> 1;
int pos = -1;
moveR(mid - 1);
long long val = 0;
for (int i = min(mid-1, R); i >= L; i--) {
moveL(i + 1);
if ((int) pq.size() == m - 2) {
if (val < now + a[i].second + 2 * a[i].first) {
val = now + a[i].second + 2 * a[i].first;
pos = i;
}
}
}
ans = max(ans, val + a[mid].second - 2 * a[mid].first);
solve(l, mid - 1, L, pos);
solve(mid + 1, r, pos, R);
};
solve(m-1, n-1, 0, n-1);
cout << ans;
}
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... |