#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define int long long
pair <int, int> a[1000000];
signed main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	freopen("castle.in", "r", stdin);
	freopen("castle.out", "w", stdout);
	int n;
	cin >> n;
	int s;
	cin >> s;
	set <pair <int, int> > sss;
	int all = 0;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i].second >> a[i].first;
		a[i].first = min(a[i].first, a[i].second);
		all += a[i].second;
		sss.insert(a[i]);
	}
	int dop = 0;
	while (s-- && sss.size()) {
		pair <int, int> cur = *sss.rbegin();
		sss.erase(--sss.end());
		dop += cur.first;
		cur.second -= cur.first;
		cur.first = min(cur.first, cur.second);
		sss.insert(cur);
	}
	cout << all - dop << endl;
}