Cod sursa(job #288210)

Utilizator vladcfVlad Frasineanu vladcf Data 25 martie 2009 17:16:09
Problema Loto Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.15 kb
var n,s,i,aux,cnt,o,x,c,sin,sfin:longint;
    v,nr:array[1..100] of longint;
    f,g:text;
    ok:boolean;
begin
assign (f,'loto.in');
assign (g,'loto.out');
reset (f);
rewrite (g);
read (f,n,s);
sin:=s;
for i:=1 to n do
    read (f,v[i]);
ok:=false;
while not(ok) do
      begin
       ok:=true;
       for i:=1 to n-1 do
           if v[i]>v[i+1] then
              begin
               aux:=v[i];
               v[i]:=v[i+1];
               v[i+1]:=aux;
               ok:=false;
              end;
      end;
if v[n]*6<s then write (g,'-1')
else
begin
for i:=1 to 6 do
    nr[i]:=v[1];
s:=s-6*v[1];
ok:=false;
o:=0;
x:=1;
while not(ok) do
      begin
       if n-o>1 then
       begin
       c:=s div (v[n-o]-v[1]);
       for i:=x to c do
           nr[i]:=v[n-o];
       if (c<x) and (c>=1) then nr[x+1]:=v[n-o];
       x:=c;
       o:=o+1;
       s:=s-c*v[n-o];
       if x>=6 then ok:=true;
       end
       else
       ok:=true;
      end;
sfin:=0;
for i:=1 to 6 do
    sfin:=sfin+nr[i];
if sfin=sin then
for i:=6 downto 1 do
    write (g,nr[i],' ')
else
write (g,'-1');
end;
close (f);
close (g);
end.