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
const int LIM = 2e5;
int n, m, L, R, fVal[LIM+1], fCnt[LIM+1], pos[LIM], ans;
array<int, 2> a[LIM], b[LIM];
void add(int i, int j){
int v = a[i][1] * j; i = pos[i];
for(; i<=n; i+=i&-i){
fCnt[i] += j;
fVal[i] += v;
}
}
int get(int l, int r){
while(R < r) add(R++, 1);
while(L > l) add(--L, 1);
while(R > r) add(--R,-1);
while(L < l) add(L++,-1);
int v = m - 2, i = 0, s = 0;
for(int j=1<<19; j/=2; ){
if(i+j <= n && fCnt[i+j] <= v){
i += j;
s += fVal[i];
v -= fCnt[i];
}
}
return s;
}
void find(int lx, int rx, int l, int r){
if(rx - lx < 1) return;
int mx = (lx + rx) / 2;
array<int, 2> res = {-1, -1};
for(int i=l; i<min(r, mx-m+2); ++i){
res = max(res, {get(i+1, mx) + a[i][1] + a[mx][1] - 2 * (a[mx][0] - a[i][0])});
}
ans = max(ans, res[0]);
find(lx, mx, l, res[1] + 1);
find(mx+1, rx, res[1], r);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=0; i<n; ++i){
cin >> a[i][1] >> a[i][0];
}
sort(a, a+n);
for(int i=0; i<n; ++i){
b[i][0] = a[i][1];
b[i][1] = i;
}
sort(b, b+n);
for(int i=0; i<n; ++i){
pos[b[i][1]] = n - i;
}
find(m-1, n, 0, n-m+1);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |