#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    sum += min(x, y);
    int d = min(2 * x, y) - min(x, y);
    v.push_back(d);
  }
  sort(v.rbegin(), v.rend());
  long long sum1 = 0;
  for (int i = 0; i < v.size() && i < k; i++) {
    sum1 += v[i];
  }
  long long ans = sum1 + sum;
  cout << ans;
  return 0;
}
