#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  bool flg = false;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flg = true;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return flg ? -x : x;
}
const long long INF = 1e18;
int n, k;
int a[500010], b[500010];
int check(long long v, bool f = 0) {
  long long res = 0;
  int tot = 0;
  priority_queue<int> A;
  priority_queue<int, vector<int>, greater<int> > B;
  for (int i = n; i; i--) {
    B.push(b[i]);
    long long X = a[i] + v + B.top(), Y = A.empty() ? INF : a[i] - A.top();
    if (min(X, Y) > 0) continue;
    if (Y < X) {
      res += Y;
      A.pop();
      A.push(a[i]);
    } else {
      res += X;
      tot++;
      B.pop();
      A.push(a[i]);
    }
  }
  if (f) cout << res - k * v << endl;
  return tot;
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  long long l = 1, r = 2e9, mid;
  while (l < r) {
    mid = l + r + 1 >> 1;
    if (check(-mid) <= k)
      l = mid;
    else
      r = mid - 1;
  }
  check(-l, 1);
  return 0;
}

### Prompt

Generate 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;
inline int read() {
  int x = 0;
  bool flg = false;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flg = true;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return flg ? -x : x;
}
const long long INF = 1e18;
int n, k;
int a[500010], b[500010];
int check(long long v, bool f = 0) {
  long long res = 0;
  int tot = 0;
  priority_queue<int> A;
  priority_queue<int, vector<int>, greater<int> > B;
  for (int i = n; i; i--) {
    B.push(b[i]);
    long long X = a[i] + v + B.top(), Y = A.empty() ? INF : a[i] - A.top();
    if (min(X, Y) > 0) continue;
    if (Y < X) {
      res += Y;
      A.pop();
      A.push(a[i]);
    } else {
      res += X;
      tot++;
      B.pop();
      A.push(a[i]);
    }
  }
  if (f) cout << res - k * v << endl;
  return tot;
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  long long l = 1, r = 2e9, mid;
  while (l < r) {
    mid = l + r + 1 >> 1;
    if (check(-mid) <= k)
      l = mid;
    else
      r = mid - 1;
  }
  check(-l, 1);
  return 0;
}

```