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

using namespace std;

int main()
{
	freopen("castle.in", "r", stdin);
	freopen("castle.out", "w", stdout);
	int n, s, ans = 0;
	cin >> n >> s;
	vector<pair<int, int> >a(n + 1, make_pair(0, 0));
	set<pair<int, int>>maxim;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].second >> a[i].first;
		ans += a[i].second;
		if (a[i].second < a[i].first)
			a[i].first = a[i].second;
	}
	sort(a.rbegin(), a.rend());
	for (int i = 0; i < n; i++)
	{
		while (a[i].second >= a[i].first && s != 0)
		{
			s--;
			ans -= a[i].first;
			a[i].second -= a[i].first;
		}
		swap(a[i].first, a[i].second);
		if(a[i].first > 0)
			maxim.insert(make_pair(a[i].first, i));
		while (!maxim.empty() && maxim.rbegin()->first > min(a[i + 1].first, a[i + 1].second) && s > 0)
		{
			a[maxim.rbegin()->second].first = 0;
			ans -= maxim.rbegin()->first;
			s--;
			auto lel = maxim.end();
			lel--;
			maxim.erase(lel);
		}
	}
	cout << ans;
	return 0;
}