#include <bits/stdc++.h>
using namespace std;
int n, k, a[500005], b[500005];
bool flag;
long long ans2;
struct node {
  int cnt;
  long long val;
  node(long long V = 0, int C = 0) { cnt = C, val = V; }
  bool operator>(const node &A) const { return val > A.val; }
};
int check(long long x) {
  priority_queue<node, vector<node>, greater<node> > q;
  int cnt = 0;
  ans2 = 0;
  for (int i = 1; i <= n; ++i) {
    q.push(node(a[i], 1));
    if (q.top().val + b[i] + x < 0) {
      if (flag) ans2 += q.top().val + b[i] + x;
      cnt += q.top().cnt;
      q.pop();
      q.push(node(-b[i] - x, 0));
    }
  }
  return cnt;
}
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 = -1000000000000000005, r = 1000000000000000005, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    int o = check(mid);
    if (o == k) {
      flag = true;
      check(mid);
      printf("%lld\n", ans2 - mid * k);
      return 0;
    }
    if (o < k)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  flag = true;
  check(ans);
  printf("%lld\n", ans2 - ans * k);
  return 0;
}
