Cod sursa(job #255173)

Utilizator MihaiBunBunget Mihai MihaiBun Data 8 februarie 2009 19:11:22
Problema Cuburi2 Scor 45
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.85 kb
program bigbug;
var f,g:text;
    a:array[1..250000] of longint;
    s,sum:array[0..250000] of int64;
    n,m,i,j,x,y,indice:longint;
    min,p:int64;

begin
  assign(f,'cuburi2.in');
  assign(g,'cuburi2.out');
  rewrite(g);
  reset(f);
  readln(f,n,m);
  s[0]:=0;
  for i:=1 to n do begin
                    read(f,a[i]);
                    s[i]:=s[i-1]+a[i];
                   end;
  sum[1]:=a[1];
  for i:=2 to n do sum[i]:=sum[i-1]+a[i]*i;
  for j:=1 to m do
    begin
     readln(f,x,y);
     min:=9200000000000000000;
     for i:=x to y do
        begin
        p:=i*(s[i]-s[x-1])-sum[i]+sum[x-1]+sum[y]-sum[i]-i*(s[y]-s[i]);
        if p<min then begin
                       min:=p;
                       indice:=i
                      end;
        end;

     writeln(g,indice,' ',min);
  end;
  close(f);
  close(g);
end.