#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, f;
  cin >> n >> f;
  multiset<int, greater<int>> m;
  vector<pair<int, int>> v(n);
  unsigned long long s = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v[i] = {x, y};
    s += min(x, y);
    m.insert(min(2 * x, y) - min(x, y));
  }
  int i = 0;
  for (auto x : m) {
    if (i >= f) break;
    s += x;
    i++;
  }
  cout << s;
}
