#include <bits/stdc++.h>
using namespace std;
long long k[100005], l[100005], cha[10005];
bool cmp(const long long a, const long long b) { return a > b; }
int main() {
  long long n, f;
  cin >> n >> f;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> k[i] >> l[i];
    ans += min(k[i], l[i]);
    cha[i] = min(2 * k[i], l[i]) - min(k[i], l[i]);
  }
  sort(cha + 1, cha + n + 1, cmp);
  for (long long i = 1; i <= f; i++) {
    ans += cha[i];
  }
  cout << ans;
  return 0;
}
