#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') w = -1;
  for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48);
  return w * c;
}
const int M = 5e5 + 10;
int n, m, a[M], b[M];
int main() {
  n = read();
  m = read();
  for (int i = (1); i <= (n); ++i) a[i] = read();
  for (int i = (1); i <= (n); ++i) b[i] = read();
  long long l = 0, r = 2e9;
  long long ans = 0;
  while (l <= r) {
    long long mid = (l + r >> 1);
    priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                   greater<pair<long long, int> > >
        q;
    int cnt = 0;
    long long ta = 0;
    for (int i = (1); i <= (n); ++i) {
      q.push(make_pair(a[i], 1));
      if (q.top().first + b[i] - mid <= 0) {
        cnt += q.top().second;
        ta += b[i] + q.top().first - mid;
        q.pop();
        q.push(make_pair(mid - b[i], 0));
      }
    }
    if (cnt >= m)
      ans = ta + mid * cnt, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << "\n";
  return 0;
}

### Prompt

In CPP, your task is to solve 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() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') w = -1;
  for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48);
  return w * c;
}
const int M = 5e5 + 10;
int n, m, a[M], b[M];
int main() {
  n = read();
  m = read();
  for (int i = (1); i <= (n); ++i) a[i] = read();
  for (int i = (1); i <= (n); ++i) b[i] = read();
  long long l = 0, r = 2e9;
  long long ans = 0;
  while (l <= r) {
    long long mid = (l + r >> 1);
    priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                   greater<pair<long long, int> > >
        q;
    int cnt = 0;
    long long ta = 0;
    for (int i = (1); i <= (n); ++i) {
      q.push(make_pair(a[i], 1));
      if (q.top().first + b[i] - mid <= 0) {
        cnt += q.top().second;
        ta += b[i] + q.top().first - mid;
        q.pop();
        q.push(make_pair(mid - b[i], 0));
      }
    }
    if (cnt >= m)
      ans = ta + mid * cnt, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << "\n";
  return 0;
}

```