#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<pair<ll,ll>> a(N);
for (int i = 0; i < N; ++i)
cin >> a[i].first >> a[i].second; // V, C
// Sort by color (C)
sort(a.begin(), a.end(), [](auto &x, auto &y){ return x.second < y.second; });
// Prefix sum of values
vector<ll> pref(N+1, 0);
for (int i = 0; i < N; ++i)
pref[i+1] = pref[i] + a[i].first;
ll ans = LLONG_MIN;
// Slide window of length M
for (int i = 0; i + M <= N; ++i) {
ll sumV = pref[i+M] - pref[i];
ll penalty = a[i+M-1].second - a[i].second;
ans = max(ans, sumV - penalty);
}
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... |