#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200'000;
const ll INF = 100'000'000'000;
int n, m;
ll v[N + 10], c[N + 10], ans;
pair<ll, ll> p[N + 10];
void readInput() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> p[i].second >> p[i].first;
sort(p + 1, p + n + 1);
for (int i = 1; i <= n; i++) {
c[i] = p[i].first;
v[i] = p[i].second;
}
}
ll calc(ll sum, int l, int r) {
return sum - 2ll * (c[r] - c[l]);
}
void solve() {
ans = -INF;
for (int i = 1; i + m - 1 <= n; i++) {
set<pair<ll, int>> s;
ll sum = 0;
for (int j = i; j <= i + m - 1; j++) {
s.insert({v[j], j});
sum += v[j];
}
ans = max(ans, calc(sum, i, i + m - 1));
for (int j = i + m; j <= n; j++) {
if (v[j] > s.begin() -> first) {
sum -= s.begin() -> first;
s.erase(s.begin());
s.insert({v[j], j});
sum += v[j];
}
ans = max(ans, calc(sum, i, j));
}
}
cout << ans << flush;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
readInput();
solve();
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... |