Cod sursa(job #255180)

Utilizator MihaiBunBunget Mihai MihaiBun Data 8 februarie 2009 19:31:55
Problema Cuburi2 Scor 47
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.96 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;
   readln(f);
   for j:=1 to m do
    begin
     readln(f,x,y);
     sum[x]:=0;
     for i:=x+1 to y do begin
                         p:=a[i]*(i-x);
                         sum[x]:=sum[x]+p;
                        end;
     min:=sum[x];
     indice:=x;
     for i:=x+1 to y do
     begin
       sum[i]:=sum[i-1]+s[i-1]-s[x-1]-s[y]+s[i-1];
       if sum[i]<=min then begin
                           min:=sum[i];
                           indice:=i
                          end;
     end;
     writeln(g,indice,' ',min);
  end;
  close(f);
  close(g);
end.