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 int long long
#define whatis(x) cout<<"line "<<__LINE__<<": "<<#x<<" is "<<(x)<<endl
//#define whatis(x) 1210
int v[201210], c[201210];
using ii = pair<int,int>;
struct topm {
int m = 0, ans = 0, sz = 0; multiset<int> s1, s2;
priority_queue<ii> pq1, pq2;
bitset<201210> in1, in2;
topm() {}
void push(int x) {
pq1.push({-v[x],x});
++sz;
ans += v[x];
in1[x] = 1;
}
void pop(int x) {
in2[x]=0;
if(in1[x]) ans-=v[x],in1[x]=0,--sz;
}
int val() {
while(sz<m && pq2.size()) {
while(pq2.size()&&!in2[pq2.top().second]) pq2.pop();
if(!pq2.size()) break;
ans += pq2.top().first;
pq1.push({-pq2.top().first,pq2.top().second});
++sz;
in2[pq2.top().second]=0;in1[pq2.top().second]=1;
pq2.pop();
}
while(sz>m) {
while(pq1.size()&&!in1[pq1.top().second]) pq1.pop();
ans += pq1.top().first;
pq2.push({-pq1.top().first,pq1.top().second});
in1[pq1.top().second]=0;in2[pq1.top().second]=1;
pq1.pop();
--sz;
}
return (sz>=m?ans:-1e10);
}
} idk;
int l = 0, r = -1, m, ans = 0;
int qry(int x, int y) {
while(r<y) idk.push(++r);
while(l>x) idk.push(--l);
while(r>y) idk.pop(r--);
while(l<x) idk.pop(l++);
return idk.val()+2*(c[x]-c[y]);
}
int dp[201210];
void dnc(int l, int r, int optl, int optr) {
if(l>r) return;
int mi = l+r>>1;
pair<int,int> best = {-1e10,-1};
for(int k=optl;k<=min(mi,optr);k++) best=max(best,{qry(k,mi),k});
dp[mi]=best.first;
int opt=best.second;
dnc(l,mi-1,optl,opt);
dnc(mi+1,r,opt,optr);
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n >> m;
idk.m = m;
pair<int,int> stuff[n];
for(int i=0;i<n;i++) cin>>stuff[i].second>>stuff[i].first;
sort(stuff,stuff+n);
for(int i=0;i<n;i++) v[i]=stuff[i].second,c[i]=stuff[i].first;
dnc(m-1,n-1,0,n-1);
cout << *max_element(dp+m-1,dp+n);
}
Compilation message (stderr)
cake3.cpp: In function 'void dnc(long long int, long long int, long long int, long long int)':
cake3.cpp:55:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mi = l+r>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |