#include <bits/stdc++.h>
using namespace std;
struct day {
  int stock;
  int client;
  int bonus;
} days[100005];
int cnt_total, cnt_sellout;
int main() {
  long long regular_sum = 0;
  cin >> cnt_total >> cnt_sellout;
  for (int i = 0; i < cnt_total; i++) {
    auto &d = days[i];
    cin >> d.stock >> d.client;
    long long regular = min(d.stock, d.client);
    d.bonus = min(d.stock * 2, d.client) - regular;
    regular_sum += regular;
  }
  sort(days, days + cnt_total,
       [](const day &a, const day &b) { return a.bonus > b.bonus; });
  for (int i = 0; i < cnt_sellout; i++) {
    regular_sum += days[i].bonus;
  }
  cout << regular_sum << endl;
}
