#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
  return p1 == p2 &&
                 (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
             ? -1
             : *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T neg = 0;
  while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
  return (!x) ? void() : (recursive_print(x / 10), putc(x % 10 ^ 48), void());
}
template <typename T>
void print(T x) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
}
template <typename T>
void print(T x, char c) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
  putc(c);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
}  // namespace fastio
const int MAXN = 5e5;
int n, k, a[MAXN + 5], b[MAXN + 5];
long long ans = 0;
bool check(long long mid) {
  priority_queue<pair<int, int> > q;
  long long sum = 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    q.push(make_pair(-a[i], 0));
    long long val = b[i] - mid - q.top().first;
    if (val < 0) sum += val, q.pop(), q.push(make_pair(b[i] - mid, 1));
  }
  while (!q.empty()) cnt += q.top().second, q.pop();
  if (cnt >= k)
    return ans = sum + mid * k, 1;
  else
    return 0;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long l = 0, r = 2e9, p = -1;
  while (l <= r) {
    long long mid = 0ll + l + r >> 1;
    if (check(mid))
      p = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  assert(~p);
  check(p);
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Please formulate 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;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
  return p1 == p2 &&
                 (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
             ? -1
             : *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T neg = 0;
  while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
  return (!x) ? void() : (recursive_print(x / 10), putc(x % 10 ^ 48), void());
}
template <typename T>
void print(T x) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
}
template <typename T>
void print(T x, char c) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
  putc(c);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
}  // namespace fastio
const int MAXN = 5e5;
int n, k, a[MAXN + 5], b[MAXN + 5];
long long ans = 0;
bool check(long long mid) {
  priority_queue<pair<int, int> > q;
  long long sum = 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    q.push(make_pair(-a[i], 0));
    long long val = b[i] - mid - q.top().first;
    if (val < 0) sum += val, q.pop(), q.push(make_pair(b[i] - mid, 1));
  }
  while (!q.empty()) cnt += q.top().second, q.pop();
  if (cnt >= k)
    return ans = sum + mid * k, 1;
  else
    return 0;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long l = 0, r = 2e9, p = -1;
  while (l <= r) {
    long long mid = 0ll + l + r >> 1;
    if (check(mid))
      p = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  assert(~p);
  check(p);
  printf("%lld\n", ans);
  return 0;
}

```