Cod sursa(job #18424)

Utilizator th3whu2Breta Ionut th3whu2 Data 18 februarie 2007 12:09:02
Problema Tricouri Scor 10
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasa a 9-a si gimnaziu Marime 1 kb
var f,g:text;
test,fz,ok1,ok2,axx,s,x,k,q,w,h,maxim,poz,i,n,m:longint;
a,e,r:array[1..1000] of integer;
begin
   assign(f,'tricouri.in');reset(f);
    assign(g,'tricouri.out');rewrite(g);
 read(f,n);
  read(f,m);
 for i:=1 to n do
  read(f,a[i]);
 k:=0;
  fz:=0;
      repeat
         inc(k);
         read(f,q);
          read(f,w);
for i:=1 to h do
r[i]:=0;
h:=0;
for i:=1 to n do
e[i]:=a[i];
repeat
maxim:=0;
poz:=0;
for i:=1 to n do
               if e[i]>maxim then begin
maxim:=e[i];
poz:=i;
end;
inc(h);
        r[h]:=maxim;
e[poz]:=-1;
until h=q;
s:=0;
for i:=1 to h do
s:=s+r[i];
x:=0;
if s mod w=0 then writeln(g,s)
             else begin
             axx:=s;
             repeat
ok1:=0;
ok2:=0;
inc(x);
axx:=s-x;
      if (axx mod w=0) then inc(ok1);
          for i:=1 to n do
            if x=a[i] then inc(ok2);
until (ok1<>0)and(ok2<>0);
       if axx<>0 then writeln(g,axx)
                 else writeln(g,'-1');
end;
until k=m;
close(f);
close(g);
end.