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>
#define int int_fast32_t
using namespace std;
typedef pair<int,int> pii;
#define rep(i,j,k) for(int i=(j);i<(k);++i)
#define f first
#define s second
#define umax(x,y) (x=max(x,(y)))
#define endl '\n'
#define ios cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)
//
const int maxn=2e5+9,inf=2e9;
int n,m,ll=0,rr=0,sum=0,res=-inf;
pii a[maxn];
multiset<int>s1,s2;
void ins(int x){
s2.insert(x),sum+=x;
auto it=s2.begin();
s1.insert(*it),s2.erase(it),sum-=*it;
}
void rem(int x){
if(s1.count(x))s1.erase(s1.find(x));
else{
s2.erase(s2.find(x)),sum-=x;
auto it=--s1.end();
s2.insert(*it),sum+=*it,s1.erase(it);
}
}
void sol(int l1,int r1,int l2,int r2){
if(clock()>3.9*CLOCKS_PER_SEC){
cout<<res<<endl;
exit(0);
}
if(l1>r1)return;
int mid=(l1+r1)/2,k=max(mid+m-1,l2);
while(rr<k)ins(a[rr++].s);
while(ll>mid)ins(a[--ll].s);
while(rr>k)rem(a[--rr].s);
while(ll<mid)rem(a[ll++].s);
pii opt={sum-2*(a[l2-1].f-a[mid].f),k};
rep(i,k,r2){
ins(a[rr++].s);
umax(opt,pii(sum-2*(a[i].f-a[mid].f),i+1));
}
umax(res,opt.f);
sol(l1,mid-1,l2,opt.s);
sol(mid+1,r1,opt.s,r2);
}
main(){
ios;
cin>>n>>m;
rep(i,0,n)cin>>a[i].s>>a[i].f;
sort(a,a+n);
rep(i,0,m)s2.insert(-inf),sum-=inf;
sol(0,n-m,m,n);
cout<<res<<endl;
}
Compilation message (stderr)
cake3.cpp:53:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |