#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
template <typename T>
void read(T &x) {
  T flag = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flag = -1;
  for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  x *= flag;
}
long long n, a[maxn], b[maxn], k;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    q;
long long ans, num;
long long check(long long m) {
  while (!q.empty()) q.pop();
  ans = num = 0;
  for (long long i = 1; i <= n; i++) {
    q.push(make_pair(a[i], 0));
    long long val = q.top().first;
    long long tmp = b[i] + val - m;
    if (tmp < 0) {
      ans += tmp;
      num += q.top().second == 0;
      q.pop();
      q.push(make_pair(-b[i] + m, i));
    }
  }
  return num;
}
int main() {
  read(n);
  read(k);
  for (long long i = 1; i <= n; i++) read(a[i]);
  for (long long i = 1; i <= n; i++) read(b[i]);
  long long l = 0, r = 2e9, res = 2e9;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    long long v = check(mid);
    if (v > k)
      r = mid - 1;
    else
      res = mid, l = mid + 1;
  }
  check(res);
  printf("%lld\n", ans + k * res);
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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 maxn = 500005;
template <typename T>
void read(T &x) {
  T flag = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flag = -1;
  for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  x *= flag;
}
long long n, a[maxn], b[maxn], k;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    q;
long long ans, num;
long long check(long long m) {
  while (!q.empty()) q.pop();
  ans = num = 0;
  for (long long i = 1; i <= n; i++) {
    q.push(make_pair(a[i], 0));
    long long val = q.top().first;
    long long tmp = b[i] + val - m;
    if (tmp < 0) {
      ans += tmp;
      num += q.top().second == 0;
      q.pop();
      q.push(make_pair(-b[i] + m, i));
    }
  }
  return num;
}
int main() {
  read(n);
  read(k);
  for (long long i = 1; i <= n; i++) read(a[i]);
  for (long long i = 1; i <= n; i++) read(b[i]);
  long long l = 0, r = 2e9, res = 2e9;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    long long v = check(mid);
    if (v > k)
      r = mid - 1;
    else
      res = mid, l = mid + 1;
  }
  check(res);
  printf("%lld\n", ans + k * res);
  return 0;
}

```