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>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
pair<int, int> a[N];
int n, m;
long long ans = -2e18;
struct node {
    node * left;
    node * right;
    int cnt;
    long long sum;
    node () : cnt(0), sum(0), left(nullptr), right(nullptr) {}
};
void build(node *p, int l, int r) {
    if (l == r) return;
    int mid = l + r >> 1;
    p -> left = new node(); p -> right = new node();
    build(p -> left, l, mid);
    build(p -> right, mid + 1, r);
}
void up(node *p, int l, int r, int pos, int val) {
    if (l == r) {
        p -> sum += val;
        p -> cnt ++;
        return;
    }
    int mid = l + r >> 1;
    if (pos <= mid) {
        p -> left = new node(*p -> left);
        up(p -> left, l, mid, pos, val);
    }
    else {
        p -> right = new node(*p -> right);
        up(p -> right, mid + 1, r, pos, val);
    }
    p -> sum = p -> left -> sum + p -> right -> sum;
    p -> cnt = p -> left -> cnt + p -> right -> cnt;
}
long long query(node *p1, node *p2, int l, int r, int k) {
    if (l == r) return p2 -> sum - p1 -> sum;
    int tmp = p2 -> right -> cnt - p1 -> right -> cnt;
    int mid = l + r >> 1;
    if (k > tmp) return p2 -> right -> sum - p1 -> right -> sum + query(p1 -> left, p2 -> left, l, mid, k - tmp);
    return query(p1 -> right, p2 -> right, mid + 1, r, k);
}
node * it[N];
long long calc(int i, int j) {
    if (j - i + 1 < m) return -1e18;
    long long tmp = a[i].first + a[j].first;
    if (m - 2 > 0) tmp += query(it[i], it[j - 1], 1, n, m - 2);
    return tmp - (a[j].second - a[i].second) * 2;
}
void solve(int l, int r, int from, int to) {
    if (l > r) return;
    int mid = l + r >> 1;
    long long f = -2e18, g = 0;
    for(int j = from; j <= to && j <= mid; j++) {
        long long tmp = calc(j, mid);
        if (tmp > f) {
            f = tmp;
            g = j;
        }
    }
    ans = max(ans, f);
    solve(l, mid - 1, from, g);
    solve(mid + 1, r, g, to);
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> m;
    vector<ii> rrh;
    for(int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
    sort(a + 1, a + n + 1, [] (const ii &x, const ii &y) {
         return x.se < y.se;
         });
    for(int i = 1; i <= n; i++)  rrh.emplace_back(a[i].first, i);
    sort(rrh.begin(), rrh.end());
    it[0] = new node(); build(it[0], 1, n);
    for(int i = 1; i <= n; i++) {
        it[i] = new node(*it[i - 1]);
        up(it[i], 1, n, upper_bound(rrh.begin(), rrh.end(), make_pair(a[i].first, i)) - rrh.begin(), a[i].first);
    }
    solve(1, n, 1, n);
    cout << ans;
}
Compilation message (stderr)
cake3.cpp: In constructor 'node::node()':
cake3.cpp:16:15: warning: 'node::sum' will be initialized after [-Wreorder]
   16 |     long long sum;
      |               ^~~
cake3.cpp:13:12: warning:   'node* node::left' [-Wreorder]
   13 |     node * left;
      |            ^~~~
cake3.cpp:17:5: warning:   when initialized here [-Wreorder]
   17 |     node () : cnt(0), sum(0), left(nullptr), right(nullptr) {}
      |     ^~~~
cake3.cpp: In function 'void build(node*, int, int)':
cake3.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |     int mid = l + r >> 1;
      |               ~~^~~
cake3.cpp: In function 'void up(node*, int, int, int, int)':
cake3.cpp:34:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |     int mid = l + r >> 1;
      |               ~~^~~
cake3.cpp: In function 'long long int query(node*, node*, int, int, int)':
cake3.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |     int mid = l + r >> 1;
      |               ~~^~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:65:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   65 |     int mid = l + r >> 1;
      |               ~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:82:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:83:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:86:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:87:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |