#include <bits/stdc++.h>
using namespace std;
bool mycomp(vector<long long int> a, vector<long long int> b) {
  long long int acost = min(2 * a[0], a[1]) - min(a[0], a[1]);
  long long int bcost = min(2 * b[0], b[1]) - min(b[0], b[1]);
  return acost > bcost;
}
long long int fn(vector<vector<long long int> > arr, long long int n,
                 long long int f) {
  sort(arr.begin(), arr.end(), mycomp);
  long long int productSold = 0;
  for (long long int i = 0; i < f; i++) {
    productSold += min(2 * arr[i][0], arr[i][1]);
  }
  for (long long int i = f; i < n; i++) {
    productSold += min(arr[i][0], arr[i][1]);
  }
  return productSold;
}
int32_t main() {
  long long int n, f;
  cin >> n >> f;
  vector<vector<long long int> > arr(n);
  for (long long int i = 0; i < n; i++) {
    long long int k, l;
    cin >> k >> l;
    arr[i].push_back(k);
    arr[i].push_back(l);
  }
  long long int ans = fn(arr, n, f);
  cout << ans;
}
