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;
struct node
{
    int l, r, v;
    long long sum;
}nd;
node pst[5000010];
pair<int, int>A[200010];
map<int, int>x, y;
map<int, int>::iterator it;
int t, n, q, a, b, c, d, i, j, sz, m, rt, cnt, root[200010], opt[200010], X[200010], Y[200010];
long long dap=-1e18;
void make_node(){pst[sz++]={-1, -1, 0, 0};}
void init(int id, int s, int e)
{
    if(s==e)return;
    int m=s+e>>1;
    if(pst[id].l==-1)
    {
        pst[id].l=sz;
        make_node();
    }
    init(pst[id].l, s, m);
    if(pst[id].r==-1)
    {
        pst[id].r=sz;
        make_node();
    }
    init(pst[id].r, m+1, e);
}
void update(int prev, int id, int s, int e, int x)
{
    pst[id].v=pst[prev].v+1;
    pst[id].sum=pst[prev].sum+Y[x];
    if(s==e)return;
    int m=s+e>>1;
    if(x<=m)
    {
        pst[id].l=sz;
        make_node();
        pst[id].r=pst[prev].r;
        update(pst[prev].l, pst[id].l, s, m, x);
    }
    else
    {
        pst[id].r=sz;
        make_node();
        pst[id].l=pst[prev].l;
        update(pst[prev].r, pst[id].r, m+1, e, x);
    }
}
long long get(int prev, int id, int s, int e, int k)
{
    if(s==e)return min(k, (int)(pst[id].sum-pst[prev].sum)/Y[s])*Y[s];
    long long d=pst[pst[id].r].sum-pst[pst[prev].r].sum;
    int ee=pst[pst[id].r].v-pst[pst[prev].r].v;
    int m=s+e>>1;
    if(k<=ee)return get(pst[prev].r, pst[id].r, m+1, e, k);
    else return d+get(pst[prev].l, pst[id].l, s, m, k-ee);
}
void dnc(int s, int e, int l, int r)
{
    if(s>e)return;
    int mid=s+e>>1;
    long long ans=-1e18;
    int pos;
    for(int i=max(l, mid+m-1);i<=r;i++)
    {
        long long res=get(root[mid], root[i-1], 1, cnt, m-2)+Y[A[mid].second]+Y[A[i].second]-(X[A[i].first]-X[A[mid].first]);
        if(ans<res)
        {
            ans=res;
            pos=i;
        }
    }
    dap=max(dap, ans);
    opt[mid]=pos;
    dnc(s, mid-1, l, pos);
    dnc(mid+1, e, pos, r);
}
main()
{
    for(scanf("%d %d", &n, &m);i++<n;)
    {
        scanf("%d %d", &b, &a);
        a*=2;
        A[i].first=a,A[i].second=b;
        x[a]++;y[b]++;
    }
    sort(A+1, A+n+1);
    for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
    for(it=y.begin();it!=y.end();it++)it->second=++cnt,Y[cnt]=it->first;
    make_node();
    init(0, 1, cnt);
    for(i=0;i++<n;)
    {
        A[i].first=x[A[i].first];
        A[i].second=y[A[i].second];
        root[i]=sz;
        make_node();
        update(root[i-1], root[i], 1, cnt, A[i].second);
    }
    dnc(1, n-m+1, 1, n);
    cout<<dap;
}
Compilation message (stderr)
cake3.cpp: In function 'void init(int, int, int)':
cake3.cpp:18:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   18 |     int m=s+e>>1;
      |           ~^~
cake3.cpp: In function 'void update(int, int, int, int, int)':
cake3.cpp:37:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |     int m=s+e>>1;
      |           ~^~
cake3.cpp: In function 'long long int get(int, int, int, int, int)':
cake3.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |     int m=s+e>>1;
      |           ~^~
cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:65:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   65 |     int mid=s+e>>1;
      |             ~^~
cake3.cpp: At global scope:
cake3.cpp:82:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   82 | main()
      |      ^
cake3.cpp: In function 'int main()':
cake3.cpp:92:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   92 |     for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
      |     ^~~
cake3.cpp:92:73: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   92 |     for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
      |                                                                         ^~~
cake3.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |     for(scanf("%d %d", &n, &m);i++<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |         scanf("%d %d", &b, &a);
      |         ~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:79:8: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   79 |     dnc(s, mid-1, l, pos);
      |     ~~~^~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |