#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  long long int item[n], client[n];
  long long int total = 0;
  vector<pair<long long int, long long int> > extra;
  for (int i = 0; i < n; i++) {
    cin >> item[i] >> client[i];
    if (item[i] == 0 || client[i] == 0 || client[i] <= item[i])
      total += min(item[i], client[i]);
    else {
      extra.push_back(
          make_pair(min(2 * item[i], client[i]) - min(item[i], client[i]), i));
    }
  }
  sort(extra.begin(), extra.end());
  reverse(extra.begin(), extra.end());
  for (int i = 0; i < extra.size(); i++) {
    int idx = extra[i].second;
    if (m > 0) {
      total += min(2 * item[idx], client[idx]);
      m--;
    } else
      total += min(item[idx], client[idx]);
  }
  cout << total << endl;
  return 0;
}
