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;
typedef long long ll;
const int MAXN = 200005;
int n, m;
ll ans[MAXN];
vector<array<ll, 2>> c; // c, v
ll solve2(int r, int optl, int optr) {
// l in [optl, optr]
priority_queue<ll, vector<ll>, greater<ll>> q; // v
ll sum = 0;
for (int i = r; i > r-m; i--) {
q.push(c[i][1]);
sum += c[i][1];
}
ll ans = sum - 2 * (c[r][0] - c[r - m + 1][0]);
for (int i = r-m; i >= optl; i--) {
q.push(c[i][1]);
sum += c[i][1];
sum -= q.top();
q.pop();
ans = max(ans, sum - 2*(c[r][0] - c[i][0]));
}
return ans;
}
// void solve(int l, int r, int optl, int optr) {
// int mid = (l + r) / 2;
// // solve for r
// priority_queue<ll, vector<ll>, greater<ll>> q;
// ll sum = 0;
// for (int i = r; i > r - m; i--) {
// q.push(c[i][1]);
// sum += c[i][1];
// }
// for (int i = r - m; i > 0; i--) {
// }
// // recurse
// solve(l, mid, optl, opt);
// solve(mid + 1, r, opt, optr);
// }
int main() {
cin >> n >> m;
c.resize(n);
for (int i = 0; i < n; i++) cin >> c[i][1] >> c[i][0];
sort(c.begin(), c.end());
ll ans = -1e18;
for (int i = m-1; i < n; i++) {
ans = max(ans, solve2(i, 0, n-1));
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |