#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
inline long long gcd(long long a, long long b) {
  return !b ? a : gcd(b, a % b);
}
inline long long q_pow(long long a, long long x) {
  long long ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
int a[N], b[N];
int main() {
  re(n);
  re(m);
  for (int i = 1; i <= n; i++) re(a[i]);
  for (int i = 1; i <= n; i++) re(b[i]);
  long long l = 0, r = 2e9;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    priority_queue<pair<int, int>, vector<pair<int, int> >,
                   greater<pair<int, int> > >
        q;
    long long ans = 0;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      q.push({a[i] - mid, 0});
      auto top = q.top();
      int val = top.first + b[i];
      if (val < 0) {
        ans += val;
        if (!top.second) cnt++;
        q.pop();
        q.push({-b[i], 1});
      }
    }
    if (cnt == m)
      return printf("%lld\n", 1ll * cnt * mid + ans), 0;
    else if (cnt < m)
      l = mid + 1;
    else
      r = mid - 1;
  }
}

### Prompt

Construct a cpp code solution to the problem outlined:

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;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
inline long long gcd(long long a, long long b) {
  return !b ? a : gcd(b, a % b);
}
inline long long q_pow(long long a, long long x) {
  long long ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
int a[N], b[N];
int main() {
  re(n);
  re(m);
  for (int i = 1; i <= n; i++) re(a[i]);
  for (int i = 1; i <= n; i++) re(b[i]);
  long long l = 0, r = 2e9;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    priority_queue<pair<int, int>, vector<pair<int, int> >,
                   greater<pair<int, int> > >
        q;
    long long ans = 0;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      q.push({a[i] - mid, 0});
      auto top = q.top();
      int val = top.first + b[i];
      if (val < 0) {
        ans += val;
        if (!top.second) cnt++;
        q.pop();
        q.push({-b[i], 1});
      }
    }
    if (cnt == m)
      return printf("%lld\n", 1ll * cnt * mid + ans), 0;
    else if (cnt < m)
      l = mid + 1;
    else
      r = mid - 1;
  }
}

```