#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:836777216")
using namespace std;
int l[100005], k[100005];
vector<pair<int, int> > p;
int main(void) {
  int n, f;
  scanf("%d%d", &n, &f);
  for (int(i) = 0; (i) < (n); (i)++) {
    scanf("%d%d", &l[i], &k[i]);
    int prof = min(2 * l[i], k[i]) - min(l[i], k[i]);
    p.push_back(make_pair(prof, i));
  }
  sort((p).begin(), (p).end());
  reverse((p).begin(), (p).end());
  long long ans = 0;
  for (int(i) = 0; (i) < (n); (i)++) {
    if (i < f) {
      ans += min(2 * l[p[i].second], k[p[i].second]);
    } else {
      ans += min(l[p[i].second], k[p[i].second]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
