#include <bits/stdc++.h>
using namespace std;
const long long M = 500005;
long long read() {
  long long x = 0, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') f = -1;
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
long long n, m, ans, res, num, a[M], b[M];
struct node {
  long long c, x;
  bool operator<(const node &b) const { return c > b.c; }
};
void work(long long mid) {
  priority_queue<node> q;
  res = num = 0;
  for (long long i = 1; i <= n; i++) {
    long long x = b[i] - mid;
    q.push(node{a[i], 1});
    node t = q.top();
    if (x + t.c <= 0) {
      q.pop();
      res += x + t.c;
      num += t.x;
      q.push(node{-x, 0});
    }
  }
}
signed main() {
  n = read();
  m = read();
  for (long long i = 1; i <= n; i++) a[i] = read();
  for (long long i = 1; i <= n; i++) b[i] = read();
  long long l = 0, r = 2e9;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    work(mid);
    if (num >= m) {
      ans = res + mid * m;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%lld\n", ans);
}
