Cod sursa(job #255176)

Utilizator MihaiBunBunget Mihai MihaiBun Data 8 februarie 2009 19:21:26
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;
  sum[0]:=0;
  for i:=1 to n do begin
                    read(f,a[i]);
                    s[i]:=s[i-1]+a[i];
                    sum[i]:=sum[i-1]+a[i]*i
                   end;
  for j:=1 to m do
    begin
     readln(f,x,y);
     min:=9223372036854775807;
     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.