#include <bits/stdc++.h>
using namespace std;
const int MX = 123456;
vector<int> v1;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  int n, f;
  long long sm = 0;
  cin >> n >> f;
  for (int i = 0, a, b; i < n; i++) {
    cin >> a >> b;
    sm += min(a, b);
    v1.push_back(min(2 * a, b) - min(a, b));
  }
  sort(v1.rbegin(), v1.rend());
  for (int i = 0; i < f; i++) sm += v1[i];
  cout << sm << endl;
  return 0;
}
