#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, mxn = 2e5 + 5;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  if (min(2 * a.first, a.second) - a.first >
      min(2 * b.first, b.second) - b.first)
    return 1;
  else
    return 0;
}
long long mainx() {
  long long n, f;
  cin >> n >> f;
  long long a[n], k[n];
  for (long long i = 0; i < n; i++) cin >> a[i] >> k[i];
  vector<pair<long long, long long> > fst, snd;
  for (long long i = 0; i < n; i++) {
    if (a[i] >= k[i]) {
      fst.push_back(make_pair(a[i], k[i]));
    } else {
      snd.push_back(make_pair(a[i], k[i]));
    }
  }
  long long ans = 0;
  for (auto &x : fst) {
    ans += x.second;
  }
  if ((long long)snd.size() == 0) {
    cout << ans << "\n";
    return 0;
  }
  sort(snd.begin(), snd.end(), comp);
  for (auto &x : snd) {
    if (f) {
      ans += min(2 * x.first, x.second);
      f--;
    } else {
      ans += min(x.first, x.second);
    }
  }
  cout << ans << "\n";
  return 0;
}
bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) {
  if (a.first < b.first) return true;
  if (a.first == b.first) return a.second < b.second;
  return false;
}
inline void INCLUDE() {}
int32_t main() {
  INCLUDE();
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long tt = 1;
  for (long long t = 1; t <= tt; t++) {
    mainx();
  }
  return 0;
}
