#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
int a[500111], b[500111], n, k;
long long ans;
priority_queue<int, vector<int>, greater<int> > qa;
priority_queue<int> qb;
int check(int x) {
  while (!qa.empty()) qa.pop();
  while (!qb.empty()) qb.pop();
  int ret = 0;
  ans = 0;
  for (int i = 1; i <= n; i++) {
    qa.push(a[i] - x);
    long long t1 = qa.top() + b[i] - x, t2 = 0;
    if (!qb.empty()) t2 = b[i] - x - qb.top();
    if (t1 <= t2 && t1 < 0) {
      ans += t1;
      qa.pop();
      qb.push(b[i] - x);
      ret++;
    } else if (t1 >= t2 && t2 < 0) {
      ans += t2;
      qb.pop();
      qb.push(b[i] - x);
    }
  }
  return ret;
}
int main() {
  get_int(n), get_int(k);
  for (int i = 1; i <= n; i++) get_int(a[i]);
  for (int i = 1; i <= n; i++) get_int(b[i]);
  int l = 0, r = 1e9 + 7, mid;
  while (l < r - 1) {
    mid = (l + r) >> 1;
    if (check(mid) < k)
      l = mid;
    else
      r = mid;
  }
  check(r);
  printf("%I64d\n", ans + 2ll * r * k);
  return 0;
}

### Prompt

Your task is to create a cpp solution to the following problem:

The plans for HC2 are rather far-fetched: we are just over 500 000 days away from HC2 3387, for example, and accordingly we are planning to have a couple hundred thousand problems in that edition (we hope that programming contests will become wildly more popular). The marmots need to get to work, and they could use a good plan...

Input

Same as the medium version, but the limits have changed: 1 ≤ k ≤ n ≤ 500 000.

Output

Same as the medium version.

Example

Input

8 4
3 8 7 9 9 4 6 8
2 5 9 4 3 8 9 1


Output

32

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
int a[500111], b[500111], n, k;
long long ans;
priority_queue<int, vector<int>, greater<int> > qa;
priority_queue<int> qb;
int check(int x) {
  while (!qa.empty()) qa.pop();
  while (!qb.empty()) qb.pop();
  int ret = 0;
  ans = 0;
  for (int i = 1; i <= n; i++) {
    qa.push(a[i] - x);
    long long t1 = qa.top() + b[i] - x, t2 = 0;
    if (!qb.empty()) t2 = b[i] - x - qb.top();
    if (t1 <= t2 && t1 < 0) {
      ans += t1;
      qa.pop();
      qb.push(b[i] - x);
      ret++;
    } else if (t1 >= t2 && t2 < 0) {
      ans += t2;
      qb.pop();
      qb.push(b[i] - x);
    }
  }
  return ret;
}
int main() {
  get_int(n), get_int(k);
  for (int i = 1; i <= n; i++) get_int(a[i]);
  for (int i = 1; i <= n; i++) get_int(b[i]);
  int l = 0, r = 1e9 + 7, mid;
  while (l < r - 1) {
    mid = (l + r) >> 1;
    if (check(mid) < k)
      l = mid;
    else
      r = mid;
  }
  check(r);
  printf("%I64d\n", ans + 2ll * r * k);
  return 0;
}

```