二分答案,每个时间段一个点,每个菜一个点,连边跑最大流,看是否满流即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 11000
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,h[N],num=1,T=10500,lev[N],tot=0,num1=0,cur[N];
struct edge{
int to,next,val;
}data[2100000];
inline void add(int x,int y,int val){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=0;
}
inline bool bfs(){
queue<int>q;memset(lev,0,sizeof(lev));
lev[0]=1;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(lev[y]||!data[i].val) continue;
lev[y]=lev[x]+1;q.push(y);
}
}return lev[T];
}
inline int dinic(int x,int low){
if(x==T) return low;int tmp=low;
for(int &i=cur[x];i;i=data[i].next){
int y=data[i].to;if(lev[y]!=lev[x]+1||!data[i].val) continue;
int res=dinic(y,min(tmp,data[i].val));
if(!res) lev[y]=0;else tmp-=res,data[i].val-=res,data[i^1].val+=res;
if(!tmp) return low;
}return low-tmp;
}
inline bool jud(int x){
num=1;int ans=0;
for(int i=1;i<=n;++i) data[++num].val=x,data[++num].val=0;
while(num<num1) data[++num].val=1,data[++num].val=0;
while(bfs()){memcpy(cur,h,sizeof(cur));ans+=dinic(0,inf);}
return ans==n*x;
}
int main(){
// freopen("a.in","r",stdin);
n=read();
for(int i=1;i<=n;++i) add(i,T,0);
for(int i=1;i<=n;++i){
int x=read(),y=read();tot=max(tot,y);
for(int j=x+1;j<=y;++j) add(j+n,i,1);
}for(int i=1;i<=tot;++i) add(0,i+n,1);
int l=1,r=tot/n;num1=num;
while(l<=r){
int mid=l+r>>1;
if(jud(mid)) l=mid+1;
else r=mid-1;
}printf("%d\n",(l-1)*n);
return 0;
}