#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const int MAX = 100001;
bool cmp(pair<long long int, pair<long long int, long long int> > &p1,
         pair<long long int, pair<long long int, long long int> > &p2) {
  return (p1.first > p2.first);
}
int main(int argc, char const *argv[]) {
  long long int n, k;
  cin >> n >> k;
  vector<pair<long long int, pair<long long int, long long int> > > A;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    long long int a, b;
    cin >> a >> b;
    if (a != 0 && b != 0) {
      if (a >= b)
        sum += b;
      else
        A.push_back(make_pair(min(2 * a, b) - min(a, b),
                              pair<long long int, long long int>(a, b)));
    }
  }
  sort(A.begin(), A.end(), cmp);
  for (int i = 0; i < A.size(); i++) {
    if (k != 0) {
      sum += min(2 * A[i].second.first, A[i].second.second);
      k--;
    } else
      sum += min(A[i].second.first, A[i].second.second);
  }
  cout << sum << endl;
  return 0;
}
