A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of the dishes will be served.
For i-th of the dishes he knows two integer moments in time ai and bi (in seconds from the beginning of the banquet) — when the cooks will bring the i-th dish into the hall and when they will carry it out (ai < bi). For example, if ai = 10 and bi = 11, then the i-th dish is available for eating during one second.
The dishes come in very large quantities, so it is guaranteed that as long as the dish is available for eating (i. e. while it is in the hall) it cannot run out.
The gourmet wants to try each of the n dishes and not to offend any of the cooks. Because of that the gourmet wants to eat each of the dishes for the same amount of time. During eating the gourmet can instantly switch between the dishes. Switching between dishes is allowed for him only at integer moments in time. The gourmet can eat no more than one dish simultaneously. It is allowed to return to a dish after eating any other dishes.
The gourmet wants to eat as long as possible on the banquet without violating any conditions described above. Can you help him and find out the maximum total time he can eat the dishes on the banquet?
Input
The first line of input contains an integer n (1 ≤ n ≤ 100) — the number of dishes on the banquet.
The following n lines contain information about availability of the dishes. The i-th line contains two integers ai and bi(0 ≤ ai < bi ≤ 10000) — the moments in time when the i-th dish becomes available for eating and when the i-th dish is taken away from the hall.
Output
Output should contain the only integer — the maximum total time the gourmet can eat the dishes on the banquet.
The gourmet can instantly switch between the dishes but only at integer moments in time. It is allowed to return to a dish after eating any other dishes. Also in every moment in time he can eat no more than one dish.
Sample Input
3
2 4
1 5
6 9
6
3
1 2
1 2
1 2
0
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 const int N=2010,M=50010,INF=1000000000; 7 int n,m,cnt,fir[N],fron[N],nxt[M],to[M],cap[M]; 8 int path[N],gap[N],dis[N],q[N],front,back; 9 struct Net_Flow{ 10 void Init(){ 11 memset(fir,0,sizeof(fir)); 12 memset(gap,0,sizeof(gap)); 13 memset(dis,0,sizeof(dis)); 14 front=back=cnt=1; 15 } 16 void addedge(int a,int b,int c){ 17 nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;cap[cnt]=c; 18 nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;cap[cnt]=0; 19 } 20 bool BFS(int S,int T){ 21 q[back++]=T;dis[T]=1; 22 while(front<back){ 23 int x=q[front++]; 24 for(int i=fir[x];i;i=nxt[i]) 25 if(!dis[to[i]])dis[q[back++]=to[i]]=dis[x]+1; 26 } 27 return dis[S]; 28 } 29 int ISAP(int S,int T){ 30 if(!BFS(S,T))return 0; 31 for(int i=S;i<=T;i++)gap[dis[i]]+=1; 32 for(int i=S;i<=T;i++)fron[i]=fir[i]; 33 int ret=0,f,p=S,Min; 34 while(dis[S]<=T+1){ 35 if(p==T){f=INF; 36 while(p!=S){ 37 f=min(f,cap[path[p]]); 38 p=to[path[p]^1]; 39 }ret+=f;p=T; 40 while(p!=S){ 41 cap[path[p]]-=f; 42 cap[path[p]^1]+=f; 43 p=to[path[p]^1]; 44 } 45 } 46 for(int &i=fron[p];i;i=nxt[i]) 47 if(cap[i]&&dis[p]==dis[to[i]]+1){ 48 path[p=to[i]]=i;break; 49 } 50 if(!fron[p]){ 51 if(!--gap[dis[p]])break;Min=T+1; 52 for(int i=fir[p];i;i=nxt[i]) 53 if(cap[i])Min=min(Min,dis[to[i]]); 54 gap[dis[p]=Min+1]+=1;fron[p]=fir[p]; 55 if(p!=S)p=to[path[p]^1]; 56 } 57 } 58 return ret; 59 } 60 }isap; 61 int l[N],r[N],hsh[N]; 62 int S,T,lo=1,hi,cntx; 63 bool Check(int x){ 64 isap.Init(); 65 S=0;T=n+cntx+1; 66 for(int i=1;i<=n;i++) 67 isap.addedge(S,i,x); 68 for(int i=1;i<cntx;i++) 69 isap.addedge(i+n,T,hsh[i+1]-hsh[i]); 70 for(int i=1;i<=n;i++) 71 for(int j=l[i];j!=r[i];j++) 72 isap.addedge(i,n+j,INF); 73 return isap.ISAP(S,T)==n*x; 74 } 75 int main(){ 76 scanf("%d",&n); 77 for(int i=1;i<=n;i++){ 78 scanf("%d%d",&l[i],&r[i]); 79 hi=max(hi,r[i]-l[i]); 80 hsh[++cntx]=l[i]; 81 hsh[++cntx]=r[i]; 82 } 83 sort(hsh+1,hsh+cntx+1); 84 cntx=unique(hsh+1,hsh+cntx+1)-hsh-1; 85 for(int i=1;i<=n;i++){ 86 l[i]=lower_bound(hsh+1,hsh+cntx+1,l[i])-hsh; 87 r[i]=lower_bound(hsh+1,hsh+cntx+1,r[i])-hsh; 88 } 89 while(lo<=hi){ 90 int mid=(lo+hi)>>1; 91 if(Check(mid))lo=mid+1; 92 else hi=mid-1; 93 } 94 printf("%d\n",hi*n); 95 return 0; 96 }