记录编号 21531 评测结果 AAAAAAAAAA
题目名称 YL杯超级篮球赛 最终得分 100
用户昵称 Gravatarnick09 是否通过 通过
代码语言 Pascal 运行时间 0.363 s
提交时间 2010-11-11 10:15:28 内存使用 2.40 MiB
显示代码纯文本
program ballc;
var i,j,k,m,n,e:longint;
    a,b:array[0..1,0..100000]of longint;
    w:array[0..100000]of longint;
    tot:array[0..100000]of longint;
procedure sort(x,y:longint);
var w,i,k,t,j,s:longint;
begin
     i:=x; j:=y; t:=x; k:=a[e,b[e,x]]; w:=b[e,x];
     repeat
           while (i<j)and(k<=a[e,b[e,j]]) do dec(j);
           if i<j then begin b[e,t]:=b[e,j]; t:=j; end;
           while (i<j)and(k>=a[e,b[e,i]]) do inc(i);
           if i<j then begin b[e,t]:=b[e,i]; t:=i; end;
     until i=j;
     b[e,t]:=w;
     if x<i-1 then sort(x,i-1);
     if i+1<y then sort(i+1,y);
end;
 
function f(e:longint):extended;
var i,j,k,m,x,y:longint;
    sum,min:extended;
begin
     sum:=0;
     fillchar(tot,sizeof(tot),0);
     for i:=1 to n do inc(tot[i],tot[i-1]+w[b[e,i]]);
     for i:=2 to n do sum:=sum+w[b[e,i]]*(a[e,b[e,i]]-a[e,b[e,1]]);
     min:=sum;
     for i:=2 to n do
     begin
          sum:=sum+tot[i-1]*(a[e,b[e,i]]-a[e,b[e,i-1]]);
          sum:=sum-(tot[n]-tot[i-1])*(a[e,b[e,i]]-a[e,b[e,i-1]]);
          if min>sum then min:=sum;
     end;
     exit(min);
end;

begin
     assign(input,'ballc.in');reset(input);
     assign(output,'ballc.out');rewrite(output);
     readln(n);
     for i:=1 to n do read(w[i]);
     for i:=1 to n do readln(a[0,i],a[1,i]);
     for i:=1 to n do b[0,i]:=i;
     for i:=1 to n do b[1,i]:=i;
     e:=0;
     sort(1,n);
     e:=1;
     sort(1,n);
     writeln(f(0)+f(1):0:2);
     close(input); close(output);
end.