#include <bits/stdc++.h>
using namespace std;
int a[100002];
int main() {
  int n, m, i, x, y;
  long long int ans = 0;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    ans += min(x, y);
    if (y <= x)
      a[i] = 0;
    else
      a[i] = min(y - x, x);
  }
  sort(a, a + n, greater<int>());
  for (i = 0; i < m; i++) ans += a[i];
  printf("%lld\n", ans);
  return 0;
}
