Cod sursa(job #254374)

Utilizator radupoenaruPoenaru Radu Constantin radupoenaru Data 7 februarie 2009 11:39:41
Problema Cuburi2 Scor 20
Compilator fpc Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 1.49 kb
var m,n,i,j,k,p,min,x,y,c:longint;
    a,s:array[1..20000] of longint;
    f,g:text;
begin
assign(f,'cuburi2.in');reset(f);
read(f,n,m);
for i:=1 to n do read(f,a[i]);
assign(g,'cuburi2.out');rewrite(g);
for i:=1 to m do begin
                 read(f,x,y);
                 j:=x;
                 min:=maxlongint;
                 for k:=x to y do s[k]:=0;
                 while j<=y do begin
                               c:=1;
                               for k:=j+1 to y do begin
                                                  s[j]:=s[j]+c*a[k];
                                                  if s[j]>min then break;
                                                  c:=c+1;
                                                  end;
                               c:=1;
                               for k:=j-1 downto x do begin
                                                      s[j]:=s[j]+c*a[k];
                                                      if s[j]>min then break;
                                                      c:=c+1;
                                                      end;
                               if s[j]<min then begin
                                                min:=s[j];
                                                p:=j;
                                                end;
                               j:=j+1;
                               end;
                 writeln(g,p,' ',min);
                 end;
close(f);
close(g);
end.