#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, f;
  cin >> n >> f;
  long long k[n + 1], l[n + 1], sum = 0;
  vector<pair<long long, long long> > vec;
  vector<pair<long long, pair<long long, long long> > > v;
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> l[i];
    vec.push_back(make_pair(l[i], k[i]));
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < n; i++)
    v.push_back(make_pair(
        min(2 * vec[i].second, vec[i].first) - min(vec[i].second, vec[i].first),
        make_pair(vec[i].first, vec[i].second)));
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < f; i++)
    sum += min(2 * v[i].second.second, v[i].second.first);
  for (int i = f; i < n; i++) sum += min(v[i].second.second, v[i].second.first);
  cout << sum << "\n";
  return 0;
}
