#include <bits/stdc++.h>
using namespace std;
unsigned long long int min(long long x, long long y) {
  if (x >= y) return y;
  return x;
}
struct tripple {
  long long items;
  long long customers;
  long long mindoubled;
};
void merge(tripple arr[], int l, int m, int r) {
  int i, j, k;
  int n1 = m - l + 1;
  int n2 = r - m;
  tripple *L = new tripple[n1], *R = new tripple[n2];
  for (i = 0; i < n1; i++) L[i] = arr[l + i];
  for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    if (L[i].mindoubled < R[j].mindoubled) {
      arr[k].customers = L[i].customers;
      arr[k].items = L[i].items;
      arr[k].mindoubled = L[i].mindoubled;
      i++;
    } else if (L[i].mindoubled > R[j].mindoubled) {
      arr[k].customers = R[j].customers;
      arr[k].items = R[j].items;
      arr[k].mindoubled = R[j].mindoubled;
      j++;
    } else if (min(L[i].customers, L[i].items) >=
               min(R[j].customers, R[j].items)) {
      arr[k].customers = L[i].customers;
      arr[k].items = L[i].items;
      arr[k].mindoubled = L[i].mindoubled;
      i++;
    } else {
      arr[k].customers = R[j].customers;
      arr[k].items = R[j].items;
      arr[k].mindoubled = R[j].mindoubled;
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }
  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
  }
}
void mergeSort(tripple arr[], int l, int r) {
  if (l < r) {
    int m = l + (r - l) / 2;
    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);
    merge(arr, l, m, r);
  }
}
int main() {
  long long n, f, k, l, max = 0, x = 0;
  cin >> n >> f;
  tripple* arr = new tripple[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i].items >> arr[i].customers;
    l = arr[i].customers;
    k = arr[i].items;
    if (l >= 2 * k)
      arr[i].mindoubled = k;
    else if (l >= k)
      arr[i].mindoubled = l - k;
    else
      arr[i].mindoubled = 0;
  }
  mergeSort(arr, 0, n - 1);
  for (int i = n - 1; i >= n - f; i--) {
    x = arr[i].mindoubled;
    l = arr[i].customers;
    k = arr[i].items;
    max += (x == l - k) ? l : (x == k) ? 2 * k : l;
  }
  for (int i = n - f - 1; i >= 0; i--)
    max += min(arr[i].customers, arr[i].items);
  cout << max;
  return 0;
}
