#include <bits/stdc++.h>
using namespace std;
long long t, n, m, x, y;
vector<pair<long long, pair<long long, long long>>> c;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    c.push_back({min(y - x, x), {x, y}});
  }
  sort(c.begin(), c.end());
  t = 0;
  for (int i = n - 1; i >= n - m; i--) {
    t += min(2 * c[i].second.first, c[i].second.second);
  }
  for (int i = n - m - 1; i >= 0; i--) {
    t += min(c[i].second.first, c[i].second.second);
  }
  cout << t;
  return 0;
}
