#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, f, count = 0;
  cin >> n >> f;
  long long int no[n], cus[n];
  vector<pair<long long int, long long int> > v;
  for (long long int i = 0; i < n; i++) {
    cin >> no[i] >> cus[i];
    v.push_back(make_pair(min(2 * no[i], cus[i]) - min(no[i], cus[i]), i));
  }
  sort(v.rbegin(), v.rend());
  for (long long int i = 0; i < f; i++) {
    long long int pos = v[i].second;
    count += min(2 * no[pos], cus[pos]);
  }
  for (long long int i = f; i < n; i++) {
    count += min(no[v[i].second], cus[v[i].second]);
  }
  cout << count << endl;
}
