(CodeForces

来源:互联网 发布:网络时髦用语2017最新 编辑:程序博客网 时间:2024/06/04 01:25

(CodeForces - 589F)Gourmet and Banquet

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

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.

Examples

Input

3
2 4
1 5
6 9

Output

6

Input

3
1 2
1 2
1 2

Output

0

Note

In the first example the gourmet eats the second dish for one second (from the moment in time 1 to the moment in time 2), then he eats the first dish for two seconds (from 2 to 4), then he returns to the second dish for one second (from 4 to 5). After that he eats the third dish for two seconds (from 6 to 8).

In the second example the gourmet cannot eat each dish for at least one second because there are three dishes but they are available for only one second (from 1 to 2).

题目大意:你要去品尝有n道菜,每一道菜的品尝时间必须相同(但不要求连续时间品尝),现告诉你每一道菜的上菜时间和收盘时间,要求你尽可能多的时间在品尝,问这个最多的时间是多少。

思路:二分+贪心。这两个方法的综合运用经常用到,必须引起重视。这里根据每一道菜的收盘时间排序,若收盘时间相同,按照上菜时间排序(因为先吃收盘早的可以保证这样对后面的菜的影响少)。然后去二分吃每一道菜的时间x,根据前面的贪心思想判断每一道菜是否能吃x时间,若不能说明这个x偏大了。(判断时用vis[i]记录i秒时吃了这道菜)

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=105;bool vis[10005];int n;struct node{    int l,r;}a[maxn];bool cmp(node a,node b){    return ((a.r<b.r)||(a.r==b.r&&a.l<b.l));}bool check(int x){    memset(vis,0,sizeof(vis));    for(int i=0;i<n;i++)    {        int cnt=0;        for(int j=a[i].l;j<a[i].r;j++)        {            if(!vis[j])            {                cnt++;                vis[j]=1;            }            if(cnt==x) break;        }        if(cnt<x) return false;//说明二分的答案偏大了     }    return true;}int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++) scanf("%d%d",&a[i].l,&a[i].r);        sort(a,a+n,cmp);        int lo=0,hi=10000,mid;        int ans;        while(lo<=hi)        {            mid=(lo+hi)>>1;            if(check(mid))            {                ans=mid;                lo=mid+1;            }            else hi=mid-1;        }        printf("%d\n",ans*n);    }    return 0;}