#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
const int N = 3e6 + 5, M = 2 * N, SEG = 2 << (int)ceil(log2(N));
long long n, m;
long long arr[N];
vector<long long> v;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  long long ans = 0;
  long long x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    long long mn = min(x, y);
    ans += mn;
    y -= mn;
    v.push_back(min(y, x));
  }
  sort(v.begin(), v.end());
  n = v.size();
  for (int i = 0; i < m; i++) ans += v[n - i - 1];
  cout << ans << "\n";
}
