#include <bits/stdc++.h>
using namespace std;
bool myfunction(pair<int, int> i, pair<int, int> j) {
  if (i.second == 0) {
    return false;
  }
  if (j.second == 0) {
    return true;
  }
  float ith = (i.first * 1.0) / i.second;
  float jth = (j.first * 1.0) / j.second;
  return (ith < jth);
}
bool myfunction2(pair<int, int> i, pair<int, int> j) {
  int ith, jth;
  int a = i.first;
  int b = i.second;
  int c = 2 * a;
  if (c > b) {
    ith = b - a;
  } else {
    ith = a;
  }
  int d = j.first;
  int e = j.second;
  int f = 2 * d;
  if (f > e) {
    jth = e - d;
  } else {
    jth = d;
  }
  return (ith > jth);
}
int main() {
  int n, f;
  pair<int, int> pr[100001];
  cin >> n >> f;
  for (int i = 0; i < n; i++) {
    cin >> pr[i].first >> pr[i].second;
  }
  sort(pr, pr + n, myfunction);
  int i;
  for (i = 0; i < n; i++) {
    float check = (pr[i].first * 1.0) / pr[i].second;
    if (check >= 1) {
      break;
    }
  }
  long long answer = 0;
  sort(pr, pr + i, myfunction2);
  for (int j = 0; j < f; j++) {
    answer += min((pr[j].first) * 2, pr[j].second);
  }
  for (int j = f; j < n; j++) {
    answer += min((pr[j].first), pr[j].second);
  }
  cout << answer << endl;
  return 0;
}
