# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
377161 | jass921026 | Cake 3 (JOI19_cake3) | C++14 | 1 ms | 368 KiB |
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;
#define jizz ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef pair<int,int> pii;
#define F first
#define S second
const ll MAXN=2E5+10, INF=1E18;
int n, m;
pii cake[MAXN];
ll ans[MAXN];
priority_queue<int,vector<int>,greater<int> > pq;
void solve(int l, int r, int ll, int rr){
if(l>r) return;
int mid=(l+r)/2, p=0;
while(!pq.empty()) pq.pop();
long long penalty=0, sum=0;
for(int i=ll;i<=rr;i++){
if(i<mid) continue;
penalty=2*(cake[i].F-cake[mid].F);
if(pq.size()<m){
pq.push(cake[i].S);
sum+=cake[i].S;
} else if(cake[i].S>pq.top()){
sum=sum-pq.top()+cake[i].S;
pq.pop();
pq.push(cake[i].S);
}
if(pq.size()==m&&sum-penalty>ans[mid]){
ans[mid]=sum-penalty;
p=i;
}
}
solve(l,mid-1,ll,p);
solve(mid+1,r,p,rr);
}
int main(){ jizz
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>cake[i].S>>cake[i].F;
}
sort(cake,cake+n);
for(int i=0;i<n;i++) ans[i]=-INF;
solve(0,n-m,0,n-1);
ll ANS=-INF;
for(int i=0;i<n;i++) ANS=max(ANS,ans[i]);
cout<<ANS<<"\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |