#include <bits/stdc++.h>
using namespace std;
struct inp {
  int c;
  int b;
  int a;
  int i;
};
inp inc[100000];
bool com(inp a, inp b) { return a.c < b.c; }
int main() {
  int n, f, i;
  cin >> n >> f;
  int ar[n + 1], br[n + 1], cr[n + 1];
  for (i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    inc[i].b = b;
    inc[i].a = a;
    inc[i].i = i;
    if (b > a) {
      int temp = min(b, 2 * a);
      inc[i].c = temp - a;
    } else
      inc[i].c = 0;
  }
  sort(inc + 1, inc + n + 1, com);
  long long int sum = 0;
  for (i = n; i >= 1; i--) {
    if (n - i + 1 <= f)
      sum += min(2 * inc[i].a, inc[i].b);
    else
      sum += min(inc[i].a, inc[i].b);
  }
  cout << sum << endl;
}
