#include <bits/stdc++.h>
using namespace std;
long n, f, K[100000], L[100000], S[100000], i;
long long sum, sum2, summax;
int main() {
  cin >> n >> f;
  for (i = 0; i < n; ++i) cin >> K[i] >> L[i];
  for (i = 0; i < n; ++i) sum += min(K[i], L[i]);
  for (i = 0; i < n; ++i) S[i] = min(K[i] * 2, L[i]) - min(K[i], L[i]);
  sort(S, S + n);
  for (i = n - 1; i >= n - f; --i) sum += S[i];
  cout << sum;
  return 0;
}
