#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, k = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') ch == '-' ? k = -1 : 0, ch = getchar();
  while (ch > 47 & ch <= '9') s = s * 10 + (ch ^ 48), ch = getchar();
  return s * k;
}
const int N = 5e5 + 10;
long long sum;
int cnt, a[N], b[N], n, k;
inline void greedy(int slope) {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      q;
  while (!q.empty()) q.pop();
  for (int i = 1; i <= n; ++i) {
    q.push(make_pair(1ll * a[i], 1));
    pair<long long, int> now = q.top();
    if (b[i] - slope + now.first <= 0) {
      sum += b[i] - slope + now.first;
      cnt += now.second;
      q.pop();
      q.push(make_pair(-1ll * (b[i] - slope), 0));
    }
  }
}
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();
  int l = 1, r = 2e9 + 1;
  long long ans = 0;
  while (l + 1 < r) {
    int mid = (l >> 1) + (r >> 1) + (l & r & 1);
    sum = cnt = 0;
    greedy(mid);
    if (cnt <= k) {
      l = mid;
      ans = 1ll * cnt * mid + sum;
    } else
      r = mid;
  }
  printf("%I64d\n", ans);
}

### Prompt

Your challenge is to write 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 s = 0, k = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') ch == '-' ? k = -1 : 0, ch = getchar();
  while (ch > 47 & ch <= '9') s = s * 10 + (ch ^ 48), ch = getchar();
  return s * k;
}
const int N = 5e5 + 10;
long long sum;
int cnt, a[N], b[N], n, k;
inline void greedy(int slope) {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      q;
  while (!q.empty()) q.pop();
  for (int i = 1; i <= n; ++i) {
    q.push(make_pair(1ll * a[i], 1));
    pair<long long, int> now = q.top();
    if (b[i] - slope + now.first <= 0) {
      sum += b[i] - slope + now.first;
      cnt += now.second;
      q.pop();
      q.push(make_pair(-1ll * (b[i] - slope), 0));
    }
  }
}
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();
  int l = 1, r = 2e9 + 1;
  long long ans = 0;
  while (l + 1 < r) {
    int mid = (l >> 1) + (r >> 1) + (l & r & 1);
    sum = cnt = 0;
    greedy(mid);
    if (cnt <= k) {
      l = mid;
      ans = 1ll * cnt * mid + sum;
    } else
      r = mid;
  }
  printf("%I64d\n", ans);
}

```