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;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
const int LIM=2e5+7;
pair<ll,ll>T[LIM], dp[LIM];
multiset<ll>S;
vector<pair<ll,ll>>rollback;
ll akt, n, m;
void upd(int x) {
S.insert(T[x].nd);
akt+=T[x].nd;
if(S.size()>m-2) {
rollback.pb({T[x].nd, *S.begin()});
akt-=*S.begin();
S.erase(S.begin());
} else rollback.pb({T[x].nd, -1});
}
void undo() {
if(rollback.back().nd!=-1) {
akt+=rollback.back().nd;
S.insert(rollback.back().nd);
}
akt-=rollback.back().st;
S.erase(S.find(rollback.back().st));
rollback.pop_back();
}
void solve(int l, int r, int a, int b) {
if(l>r) return;
int mid=(l+r)/2;
for(int i=mid+1; i<=min(r, a-1); ++i) upd(i);
for(int i=max(mid+1, a); i<=b; ++i) {
if(S.size()==m-2) dp[mid]=max(dp[mid], {akt+T[i].nd+T[mid].nd-2*T[i].st+2*T[mid].st, i});
upd(i);
}
for(int i=b; i>=max(mid+1, a); --i) undo();
upd(mid);
solve(l, mid-1, a, dp[mid].nd);
undo();
for(int i=min(r, a-1); i>=mid+1; --i) undo();
for(int i=max(r+1, a); i<dp[mid].nd; ++i) upd(i);
solve(mid+1, r, dp[mid].nd, b);
for(int i=dp[mid].nd-1; i>=max(r+1, a); --i) undo();
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, n) dp[i]={-INF, -1};
rep(i, n) cin >> T[i].nd >> T[i].st;
sort(T, T+n);
solve(0, n-m, 0, n-1);
ll ans=-INF;
rep(i, n-m+1) ans=max(ans, dp[i].st);
cout << ans << '\n';
}
Compilation message (stderr)
cake3.cpp: In function 'void upd(int)':
cake3.cpp:18:13: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
18 | if(S.size()>m-2) {
| ~~~~~~~~^~~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:38:14: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
38 | if(S.size()==m-2) dp[mid]=max(dp[mid], {akt+T[i].nd+T[mid].nd-2*T[i].st+2*T[mid].st, i});
| ~~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |