Cod sursa(job #215595)

Utilizator FllorynMitu Florin Danut Flloryn Data 19 octombrie 2008 16:47:57
Problema Loto Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.39 kb
program pascal;
var f,g:text;
v:array[1..100] of -100000000..100000000;
i,n,j,k,p,r,q,aux:1..100;  ok,bun:boolean; s,max:longint;

   procedure quicksort(s,d:integer);
   var a,b,ia,aux:integer;
   begin
      a:=s; b:=d;
      repeat
       while v[a]<v[b] do b:=b-1;
       aux:=v[a];
       v[a]:=v[b];
       v[b]:=aux;
       ia:=1;
       a:=a+1;
       if a<b then
          begin
            while v[a]<v[b] do a:=a+1;
            if a<>b then
               begin
               aux:=v[a];
               v[a]:=v[b];
               v[b]:=aux;
               ia:=0;
               b:=b-1;
               end;
            end;
      until a>b;
      if s<a-ia then quicksort(s,a-ia);
      if a-ia+1<d then quicksort(a-ia+1,d);
   end;

begin
assign(f,'loto.in'); reset(f);
assign(g,'loto.out'); rewrite(g);
readln(f,n,s);
max:=0;  ok:=false;
for i:=1 to n do
  begin
     read(f,v[i]);
     if v[i]>max then max:=v[i];
     if 6*v[i]=s then
        begin
           ok:=true;
           aux:=i;
        end;
  end;
if ok then
  for i:=1 to 6 do write(g,v[aux],' ')
else
if 6*max<s then write(g,-1)
else
begin
quicksort(1,n);
ok:=false;
bun:=false;
for i:=1 to n do
 begin
 if v[i]>s then
         begin
            bun:=true;
            break;
         end;
 for j:=1 to n do
    begin
    if v[i]+v[j]>s then
         begin
            bun:=true;
            break;
         end;
    for k:=1 to n do
       begin
       if v[i]+v[j]+v[k]>s then
        begin
            bun:=true;
            break;
         end;
       for p:=1 to n do
         begin
         if v[i]+v[j]+v[k]+v[p]>s then
          begin
            bun:=true;
            break;
         end;
         for q:=1 to n do
             begin
             if v[i]+v[j]+v[k]+v[p]+v[q]>s then break;
             for r:=1 to n do
             if (v[i]+v[j]+v[k]+v[p]+v[q]+v[r]=s) then
                begin
                  write(g,v[i],' ',v[j],' ',v[k],' ',v[p],' ',v[q],' ',v[r]);
                  ok:=true;
                  break;
               end;
            if (ok) or (bun) then break;
            end;
        if (ok) or (bun)then break;
        end;
      if (ok) or (bun) then break;
     end;
     if (ok) or (bun) then break;
     end;
     if (ok) or (bun) then break;
    end;

if not ok then write(g,-1);
end;
close(f);
close(g);
end.