#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
ostream &operator<<(ostream &out, const pair<T, V> x) {
  out << "{" << x.first << " : " << x.second << "}";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const set<T> x) {
  for (auto &it : x) {
    out << it << " ";
  }
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const multiset<T> x) {
  for (auto &it : x) {
    out << it << " ";
  }
  return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, const map<T, V> x) {
  for (auto &it : x) {
    out << "[" << it.first << "]"
        << " = " << it.second << "\n";
  }
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> x) {
  for (int i = 0; i < x.size() - 1; ++i) {
    out << x[i] << " ";
  }
  out << x.back();
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<vector<T> > x) {
  for (int i = 0; i < x.size() - 1; ++i) {
    out << "[" << i << "]"
        << " = {" << x[i] << "}\n";
  }
  out << "[" << x.size() - 1 << "]"
      << " = {" << x.back() << "}\n";
  return out;
}
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
vector<long long> add;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, f;
  cin >> n >> f;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long a, b;
    cin >> a >> b;
    ans += min(a, b);
    b -= a;
    add.push_back(min(a, b));
  }
  sort((add).begin(), (add).end());
  for (long long i = 0; i < f; ++i) {
    long long x = add[n - i - 1];
    if (x > 0) ans += x;
  }
  cout << ans;
  return 0;
}
