Cod sursa(job #255153)

Utilizator MihaiBunBunget Mihai MihaiBun Data 8 februarie 2009 18:54:47
Problema Cuburi2 Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.85 kb
program bgbg;
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:=9000000000000000000;
     for i:=x to y do
        begin
        p:=sum[i]-i*s[x-1]+sum[x-1]-sum[n]+sum[y]+i*(s[n]-s[y]);
        if p<min then begin
                       min:=p;
                       indice:=i
                      end;
        end;
    end;
     writeln(g,indice,' ',min);
  end;
  close(f);
  close(g);
end.