#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 500005;
int a[maxn], b[maxn], n, k;
struct info {
  long long val;
  int isa;
  info(long long A = 0, int B = 0) { val = A, isa = B; }
  bool operator<(const info& x) const {
    return val == x.val ? isa < x.isa : val > x.val;
  }
};
priority_queue<info> q;
long long sum, cnt;
void check(int thr) {
  sum = 0, cnt = 0;
  while (!q.empty()) q.pop();
  for (int i = int(1); i <= (int)(n); i++) {
    q.push(info(a[i], 1));
    info t = q.top();
    if (b[i] + t.val - thr <= 0) {
      cnt += t.isa;
      sum += b[i] + t.val - thr;
      q.pop();
      q.push(info(thr - b[i], 0));
    }
  }
}
int main() {
  n = rd(), k = rd();
  for (int i = int(1); i <= (int)(n); i++) a[i] = rd();
  for (int i = int(1); i <= (int)(n); i++) b[i] = rd();
  int l = 0, r = 2000000000;
  while (l <= r) {
    int mid = (0ll + l + r) >> 1;
    check(mid);
    if (cnt == k) return rt(sum + (long long)cnt * mid), putchar('\n'), 0;
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
}

### Prompt

Please provide a Cpp coded solution 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;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 500005;
int a[maxn], b[maxn], n, k;
struct info {
  long long val;
  int isa;
  info(long long A = 0, int B = 0) { val = A, isa = B; }
  bool operator<(const info& x) const {
    return val == x.val ? isa < x.isa : val > x.val;
  }
};
priority_queue<info> q;
long long sum, cnt;
void check(int thr) {
  sum = 0, cnt = 0;
  while (!q.empty()) q.pop();
  for (int i = int(1); i <= (int)(n); i++) {
    q.push(info(a[i], 1));
    info t = q.top();
    if (b[i] + t.val - thr <= 0) {
      cnt += t.isa;
      sum += b[i] + t.val - thr;
      q.pop();
      q.push(info(thr - b[i], 0));
    }
  }
}
int main() {
  n = rd(), k = rd();
  for (int i = int(1); i <= (int)(n); i++) a[i] = rd();
  for (int i = int(1); i <= (int)(n); i++) b[i] = rd();
  int l = 0, r = 2000000000;
  while (l <= r) {
    int mid = (0ll + l + r) >> 1;
    check(mid);
    if (cnt == k) return rt(sum + (long long)cnt * mid), putchar('\n'), 0;
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
}

```