#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, i, j, t, k, lie, m, n, o, s = 0, x, y, z, ar[100010];
  vector<long long> v;
  scanf("%lld %lld", &n, &x);
  while (n--) {
    scanf("%lld %lld", &a, &b);
    if (a >= b)
      s += b;
    else {
      s += a;
      y = a * 2;
      if (b >= y)
        v.push_back(a);
      else
        v.push_back(b - a);
    }
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  z = min(x, (long long)v.size());
  for (i = 0; i < z; i++) s += v[i];
  cout << s << "\n";
  return 0;
}
