#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_pq = priority_queue<T>;
const int MAXN = 1 << 17;
const int INF = 1 << 30;
int n, k;
long long a[MAXN], b[MAXN];
pair<long long, int> solve(long long x) {
  min_pq<long long> A;
  max_pq<long long> B;
  int i;
  long long sol = 0;
  int cnt = 0;
  for (i = 0; i < n; ++i) {
    A.push(a[i]);
    long long p = 0, q = 0;
    if (!A.empty()) p = A.top() + b[i] - x;
    if (!B.empty()) q = b[i] - B.top();
    if (p < q && p < 0) {
      A.pop();
      B.push(b[i]);
      sol += p;
      ++cnt;
    } else if (q < 0) {
      B.pop();
      B.push(b[i]);
      sol += q;
    }
  }
  return {sol, cnt};
}
int main() {
  int i;
  cin >> n >> k;
  for (i = 0; i < n; ++i) cin >> a[i];
  for (i = 0; i < n; ++i) cin >> b[i];
  long long lo = 0, hi = 1ll << 60, mid, p;
  while (hi - lo > 1) {
    mid = (lo + hi) / 2;
    p = solve(mid).second;
    if (p < k)
      lo = mid;
    else
      hi = mid;
  }
  cout << solve(hi).first + k * hi << endl;
}
