#include <bits/stdc++.h>
using namespace std;
inline void file() { freopen("test.in", "r", stdin); }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  register int ret = 0, f = 0;
  char ch = getc();
  while (!isdigit(ch)) {
    if (ch == '-') f = 1;
    ch = getc();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + ch - 48;
    ch = getc();
  }
  return f ? -ret : ret;
}
int n, k;
long long sum;
long long a[500005], b[500005];
inline int calc(long long mid) {
  priority_queue<pair<long long, bool> > q;
  register int ret = 0;
  sum = 0;
  for (register int i = 1; i <= n; ++i) {
    q.push(make_pair(-(a[i] - mid), 1));
    const long long tmp = -q.top().first + b[i];
    if (tmp < 0) {
      ret += q.top().second;
      sum += tmp;
      q.pop();
      q.push(make_pair(b[i], 0));
    }
  }
  return ret;
}
int main() {
  n = read();
  k = read();
  for (register int i = 1; i <= n; ++i) a[i] = read();
  for (register int i = 1; i <= n; ++i) b[i] = read();
  long long l = 0, r = 0x7f7f7f7f;
  while (l < r) {
    long long mid = (l + r + 1) >> 1;
    if (calc(mid) <= k)
      l = mid;
    else
      r = mid - 1;
  }
  calc(l);
  printf("%lld", sum + l * k);
  return 0;
}

### Prompt

Your task is to create 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 void file() { freopen("test.in", "r", stdin); }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  register int ret = 0, f = 0;
  char ch = getc();
  while (!isdigit(ch)) {
    if (ch == '-') f = 1;
    ch = getc();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + ch - 48;
    ch = getc();
  }
  return f ? -ret : ret;
}
int n, k;
long long sum;
long long a[500005], b[500005];
inline int calc(long long mid) {
  priority_queue<pair<long long, bool> > q;
  register int ret = 0;
  sum = 0;
  for (register int i = 1; i <= n; ++i) {
    q.push(make_pair(-(a[i] - mid), 1));
    const long long tmp = -q.top().first + b[i];
    if (tmp < 0) {
      ret += q.top().second;
      sum += tmp;
      q.pop();
      q.push(make_pair(b[i], 0));
    }
  }
  return ret;
}
int main() {
  n = read();
  k = read();
  for (register int i = 1; i <= n; ++i) a[i] = read();
  for (register int i = 1; i <= n; ++i) b[i] = read();
  long long l = 0, r = 0x7f7f7f7f;
  while (l < r) {
    long long mid = (l + r + 1) >> 1;
    if (calc(mid) <= k)
      l = mid;
    else
      r = mid - 1;
  }
  calc(l);
  printf("%lld", sum + l * k);
  return 0;
}

```