#include <bits/stdc++.h>
using namespace std;
bool pred(const pair<int, int>& a, const pair<int, int>& b) {
  int x1 = min(a.first * 2, a.second) - a.first;
  int x2 = min(b.first * 2, b.second) - b.first;
  return x1 > x2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, c;
  cin >> n >> c;
  unsigned long long int sum = 0;
  int E[n];
  for (int i = 0; i < n; ++i) {
    int k, l;
    cin >> k >> l;
    sum += min(k, l);
    E[i] = min(k * 2, l) - min(k, l);
  }
  sort(E, E + n, greater<int>());
  for (int i = 0; i < c; ++i) {
    if (E[i] <= 0) break;
    sum += E[i];
  }
  cout << sum << '\n';
  return 0;
}
