#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, f;
  cin >> n >> f;
  long long sum = 0;
  int l, k;
  priority_queue<int, vector<int>, greater<int>> pq;
  for (int i = 0; i < n; ++i) {
    cin >> k >> l;
    int sold = min(k, l);
    sum += sold;
    int doubleSold = min(k * 2, l);
    int diff = doubleSold - sold;
    if (diff > 0) {
      pq.push(diff);
      if (((int)((pq).size())) > f) pq.pop();
    }
  }
  for (int i = 0; i < f; ++i) {
    if (pq.empty()) break;
    int diff = pq.top();
    pq.pop();
    sum += diff;
  }
  cout << sum << "\n";
  return 0;
}
