This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 200005
#define maxx 8000005
ll n,m;
pll a[maxn];
ll b[maxn];
pll t[maxx];
ll root[maxx],ls[maxx],rs[maxx];
ll dp[maxn];
ll tsz = 1;
pll add(pll x,pll y){return {x.fi+y.fi,x.sc+y.sc};}
void upd(ll v,ll rv,ll tl,ll tr,ll i,pll p){
if(tl==tr){t[v] = p;return;}
ll mid = (tl+tr)/2;
if(i<=mid){
rs[v] = rs[rv];
ls[v] = ++tsz;
upd(ls[v],ls[rv],tl,mid,i,p);
}else{
ls[v] = ls[rv];
rs[v] = ++tsz;
upd(rs[v],rs[rv],mid+1,tr,i,p);
}
t[v] = add(t[ls[v]],t[rs[v]]);
}
ll get(ll v,ll rv,ll tl,ll tr,ll k){
if(k==0) return 0;
if(tl==tr) return t[v].fi-t[rv].fi;
if(t[v].sc-t[rv].sc<=k) return t[v].fi-t[rv].fi;
ll mid = (tl+tr)/2;
ll cnt = t[rs[v]].sc-t[rs[rv]].sc;
ll sum = t[rs[v]].fi-t[rs[rv]].fi;
if(cnt>k) return get(rs[v],rs[rv],mid+1,tr,k);
else return get(ls[v],ls[rv],tl,mid,k-cnt) + sum;
}
void init(ll v,ll tl,ll tr){
if(tl==tr) return;
ll mid = (tl+tr)/2;
ls[v] = ++tsz,rs[v] = ++tsz;
init(ls[v],tl,mid);
init(rs[v],mid+1,tr);
}
void reshi(ll tl,ll tr,ll optl,ll optr){
if(tl>tr) return;
ll mid = (tl+tr)/2;
ll opt = -1;
for(ll i = max(mid+m-1,optl);i<=optr;i++){
if(i-mid+1<m) continue;
ll cur = get(root[i-1],root[mid],1,n,m-2);
cur += a[i].sc+a[mid].sc-2*(a[i].fi-a[mid].fi);
if(cur>=dp[mid]){
dp[mid] = cur;
opt = i;
}
}
if(opt!=-1){
reshi(tl,mid-1,optl,opt);
reshi(mid+1,tr,opt,optr);
}else{
reshi(tl,mid-1,optl,optr);
reshi(mid+1,tr,optl,optr);
}
}
bool cmp(pll x,pll y){return x.sc<y.sc;}
map<pll,ll> mp;
set<pll> s;
void tc(){
cin >> n >> m;
for(ll i = 1;i<=n;i++) cin >> a[i].sc >> a[i].fi;
sort(a+1,a+1+n);
ll it = 0;
for(ll i = 1;i<=n;i++) s.insert({a[i].sc,i});
for(pll x : s) mp[x] = ++it;
init(1,1,n);
root[0] = 1;
for(ll i = 1;i<=n;i++){
root[i] = ++tsz;
upd(root[i],root[i-1],1,n,mp[{a[i].sc,i}],{a[i].sc,1});
}
for(ll i = 1;i<=n;i++) dp[i] = -llinf;
reshi(1,n,1,n);
ll ans = -llinf;
for(ll i = 1;i<=n;i++) ans = max(ans,dp[i]);
cout<<ans<<endl;
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
while(t--){
tc();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |