比赛 20110729 评测结果 AAAAAAAAAAAA
题目名称 最后的利益 最终得分 100
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-29 10:04:42
显示代码纯文本
var
  n,i,j,max:longint;
  g:Array[0..40000]of boolean;
  a,b,f:array[0..40000]of longint;

procedure Sort(l, r: longint);
var
  i, j, x, y: integer;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      y := b[i]; b[i] := b[j]; b[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

begin
  assign(input,'9cwy.in'); reset(input);
  assign(output,'9cwy.out'); rewrite(output);
  readln(n);
  max:=-maxlongint;
  for i:=1 to n do
  begin
    readln(a[i],b[i]);
    if b[i]>max then max:=b[i];
    g[a[i]]:=true;
  end;
  sort(1,n);
  a[0]:=-1;
  i:=n+1;
  while (max>=0) do
  begin
    f[max]:=f[max+1];
    if g[max] then
    begin
      repeat
        dec(i);
        if b[i]-a[i]+f[b[i]]>f[max] then
        f[max]:=b[i]-a[i]+f[b[i]];
      until a[i-1]<>a[i];
    end;
    dec(max);
  end;
  writeln(f[0]);
  close(input);
  close(output);
end.