#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nax = 2e5 + 10;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, f;
  cin >> n >> f;
  vector<int> k(n), l(n);
  vector<pair<int, int> > a;
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> l[i];
    a.push_back({min(2 * k[i], l[i]) - min(k[i], l[i]), i});
  }
  sort(a.rbegin(), a.rend());
  ll ans = 0;
  for (int i = 0; i < f; i++) {
    ans += min(2 * k[a[i].second], l[a[i].second]);
  }
  for (int i = f; i < n; i++) {
    ans += min(k[a[i].second], l[a[i].second]);
  }
  cout << ans << '\n';
  return 0;
}
