#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int LINF = 0x3f3f3f3f3f3f3f3f;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  return (min(2LL * a.first, 1LL * a.second) - min(a.first, a.first) >
          min(2LL * b.first, 1LL * b.second) - min(b.second, b.first));
}
long long sold;
vector<pair<int, int> > v;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, f;
  cin >> n >> f;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    if (x >= y)
      sold += y * 1LL;
    else
      v.push_back({x, y});
  }
  sort(v.begin(), v.end(), cmp);
  int i = 0;
  for (; i < f && i < v.size(); i++)
    sold += min(v[i].first * 2LL, 1LL * v[i].second);
  for (; i < v.size(); i++) sold += v[i].first;
  cout << sold;
}
