#include <bits/stdc++.h>
using namespace std;
int n, f;
long long ans;
long long k, l;
long long d[100001];
int main() {
  cin >> n >> f;
  for (int i = 1; i <= n; i++) {
    cin >> k >> l;
    d[i] = -min(k, l - k);
    ans = ans + min(k, l);
  }
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= f; i++) ans = ans + max(-d[i], (long long)(0));
  cout << ans;
  return 0;
}
