#include <bits/stdc++.h>
using namespace std;
const int N = int(300000) + 5;
long long int k, t, n, m, cnt, p, mx, sum, ans, cnt1;
long long int a[N], b[N], c[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    c[i] = min(2 * a[i], b[i]) - min(a[i], b[i]);
  }
  sort(c + 1, c + 1 + n);
  cnt = n;
  for (long long int i = 1; i <= n; i++) {
    if (i <= m) {
      ans += c[cnt--];
    }
    ans += min(a[i], b[i]);
  }
  cout << ans;
  return 0;
}
