#include <fstream>
#include <set>
#include <algorithm>

#define MOD (long long)1e10

using namespace std;

struct part
{
	int id;
	int a, k;
	long long ef;
	part(int a, int k, long long ef) : a(a), k(k), ef(ef) {}
	part(int id): id(id) {}
	bool operator <(part p) const
	{
		if (ef == p.ef)
			return id < p.id;
		return ef > p.ef;
	}
};

set<part> arr;

int main()
{
	ifstream fin("castle.in");
	ofstream fout("castle.out");
	int n, s, ans = 0;
	fin >> n >> s;
	for (int i = 0; i < n; i++)
	{
		part P(i);
		fin >> P.a >> P.k;
		ans += P.a;
		P.ef = min(P.a, P.k);
		arr.insert(P);
	}
	while (arr.size() && s > 0)
	{
		part P = *arr.begin();
		arr.erase(P);
		ans -= P.ef;
		P.a -= P.ef;
		P.ef = min(P.a, P.k);
		if (P.ef)
			arr.insert(P);
		s--;
	}
	fout << ans;
	return 0;
}