UESTC - 1045

Lovely princess


Time Limit:                                                        1000MS                       

 

Memory Limit: 65535KB

 

64bit IO Format:                            %lld & %llu                       


SubmitStatus


Description



There are n jobs you need to complete. However, complete the ith job you capability must be no less than vi. If you have completed the ith job, your capability will increase ai.

Then the question is coming, what is the minimum initial capability value if you are required to complete all of the n

Note that there is no restriction on the order you complete them. That is to say, you can decide the order by your own.


Input



The first line contains a single integer n, which is the number of jobs you need to complete.

Then each of the following n lines contains 2 integers vi and ai, which are described above.

1≤n≤1000,0≤vi≤1000000,0≤ai≤1000


Output



Print the answer in one line.


Sample Input



1
2 1


Sample Output



2


Hint



Source


The 13th UESTC Programming Contest Preliminary


//题意:输入n个数,再输入n个a[i],w[i];


给你n个任务,每完成一个一个任务你的能力值会加上对应任务的权值,完成某个任务的要求是,你的能力值要大于这个任务的a[i],问初始的能力值最小是多大?


//思路:


直接模拟。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1010
using namespace std;
struct zz
{
	int v;
	int w;
}p[N];
bool cmp(zz a,zz b)
{	
	if(a.v==b.v)
		return a.w<b.w;
	return a.v<b.v;
}
int main()
{
	int n,m,i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%d%d",&p[i].v,&p[i].w);
		sort(p,p+n,cmp);
		int k=p[0].v,kk=0;
		m=k;
		for(i=1;i<n;i++)
		{
			m+=p[i-1].w;
			if(m<p[i].v)
			{
				kk=p[i].v-m;
				k+=kk;m+=kk;
			}
		}
		printf("%d\n",k);
	}
	return 0;
}