#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long dfs(string s, char c) {
  long long n = s.length();
  if (n == 1) {
    if (c == s[0])
      return 0;
    else
      return 1;
  }
  long long lc = 0;
  for (long long i = 0; i < n / 2; i++) {
    if (s[i] != c) {
      lc += 1;
    }
  }
  long long rc = 0;
  for (long long i = n / 2; i < n; i++) {
    if (s[i] != c) {
      rc += 1;
    }
  }
  return min(dfs(s.substr(0, n / 2), c + 1) + rc,
             dfs(s.substr(n / 2, n), c + 1) + lc);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    long long n, f;
    cin >> n >> f;
    vector<long long> v;
    long long nas = 0;
    for (long long i = 0; i < n; i++) {
      long long k, l;
      cin >> k >> l;
      v.push_back(min(2 * k, l) - min(k, l));
      nas += min(k, l);
    }
    sort(v.begin(), v.end(), greater<long long>());
    for (long long i = 0; i < f; i++) {
      nas += v[i];
    }
    cout << nas;
  }
  return 0;
}
