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

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));
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].second >> a[i].first;
		ans += 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;
		}
		if (max(a[i].second, min(a[i + 1].second, a[i + 1].first)) == a[i].second && s != 0)
		{
			s--;
			ans -= a[i].second;
			a[i].second = 0;
		}
		swap(a[i].first, a[i].second);
	}
	if (ans != 0 && s != 0)
	{
		sort(a.rbegin(), a.rend());
		for (int i = 0; i < n && s > 0 && a[i].first > 0; i++)
		{
			s--;
			ans -= a[i].first;
		}
	}
	cout << ans;
	return 0;
}