#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<pii>;
using vs = vector<string>;
const double PI = acos(-1.0);
const double EPS = 1e-14;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll INFLL = 4e18;
const int MAX = 1e5;
int N, F;
ll ans;
int pos[MAX + 5];
void read() {
  cin >> N >> F;
  for (int i = 0; i < (int)N; i++) {
    int k, l;
    cin >> k >> l;
    pos[i] = (max(min(k, l - k), 0));
    ans += min(k, l);
  }
}
void solve() {
  sort(pos, pos + N, greater<int>());
  for (int i = 0; i < (int)F; i++) ans += pos[i];
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1;
  while (TC--) {
    read();
    solve();
  }
}
