#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, f;
  cin >> n >> f;
  vector<long long> v;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long k, l;
    cin >> l >> k;
    v.push_back(min(k - l, l));
    sum += min(k, l);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < f; i++) {
    if (v[n - i - 1] < 0) break;
    sum += v[n - 1 - i];
  }
  cout << sum;
  return 0;
}
