# 【DP】饥饿的牛

饥饿的牛

约翰提供B个区间的清单。一个区间是一对整数start-end,1<=start<=end<=N，表示一些连续的饲料槽，比如1-3,7-8,3-4等等。牛可以任意选择区间，但是牛选择的区间不能有重叠。

HUNGER.IN

3

1 3

7 8

3 4

HUNGER.OUT

5

=================================

=====================================

type
node=record
s,e:longint;
end;
var
b:longint;
a:array[1..2000]of node;
f:array[0..2000]of longint;
procedure init;
begin
assign(input,'hunger.in');
assign(output,'hunger.out');
reset(input); rewrite(output);
end;

procedure terminate;
begin
close(input); close(output);
halt;
end;

procedure qsort(s,t:longint);
var
i,j:longint;
x:node;
tem:node;
begin
i:=s; j:=t;
x:=a[(s+t)shr 1];
repeat
while x.e<a[j].e do dec(j);
while a[i].e<x.e do inc(i);
if i<=j then
begin
tem:=a[i]; a[i]:=a[j]; a[j]:=tem;
inc(i); dec(j);
end;
until i>j;
if i<t then qsort(i,t);
if s<j then qsort(s,j);
end;

function max(a,b:longint):longint;
begin
if a>b then exit(a);
exit(b);
end;

procedure main;
var
i,j:longint;
ans:longint;
begin
for i:=1 to b do
qsort(1,b);
fillchar(f,sizeof(f),0);
ans:=0;
for i:=1 to b do
for j:=a[i].s-1 downto 0 do
begin
f[a[i].e]:=max(f[a[i].e],f[j]+(a[i].e-a[i].s+1));
if ans<f[a[i].e] then ans:=f[a[i].e];
end;
writeln(ans);
end;

begin
init;
main;
terminate;
end.