#include<iostream>

int main()
{
	using namespace std;
	int t;
	int i;
	long long int T=1000000001;
	long long int a,b;
	int minus_=0;
	cin>>t;
	long long int counter=0;
	for(i=0;i<t;i++)
	{
		cin>>a>>b;
		if(a<=b)
		{
		counter+=a;
		minus_+=b-a;
		}
		else
		{
			if(T>b)
			T=b;
			counter+=a;
		}
		
	}
	if(minus_==0)
	{
		cout<<"0"<<endl;
		return 0;
	}
	cout<<counter-T<<endl;
	return 0;
}
