Cod sursa(job #56985)

Utilizator cezar305Mr. Noname cezar305 Data 30 aprilie 2007 21:02:57
Problema Loto Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.12 kb
type vector=array[0..1001000]of longint;
var x,p1,p2,p3:vector;
    i,j,k,n,s,g,sum:longint;
    v:array[1..130]of longint;

procedure qsort(ls,ld:longint;var v:vector);
var i,j,aux:longint;
begin
  i:=ls;j:=ld;
  while true do begin
    while (v[i]<=v[j])and(i<>j) do inc(i);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;dec(j);
    while (v[i]<=v[j])and(i<>j) do dec(j);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;inc(i);
  end;
  if j-1>ls then qsort(ls,j-1,v);
  if j+1<ld then qsort(j+1,ld,v);
end;

function gasit(n,ls,ld:longint;var v:vector):longint;
var m:longint;
begin
m:=(ls+ld)div 2;
if v[m]=n then
        begin
        gasit:=m;
        exit;
        end;
if ls>ld then
        begin
        gasit:=0;
        exit;
        end;
if n<v[m] then
        gasit:=gasit(n,ls,m-1,x)
else
        gasit:=gasit(n,m+1,ld,x);
end;

begin
assign(input,'loto.in');reset(input);
assign(output,'loto.out');rewrite(output);
readln(n,s);
for i:=1 to n do
        read(v[i]);
for i:=1 to n do
        for j:=1 to n do
                for k:=1 to n do
                        begin
                        inc(x[0]);
                        x[x[0]]:=v[i]+v[j]+v[k];
                        p1[x[0]]:=i;
                        p2[x[0]]:=j;
                        p3[x[0]]:=k;
                        end;
qsort(1,x[0],x);
for i:=1 to n do
        for j:=1 to n do
                for k:=1 to n do
                        begin
                        sum:=v[i]+v[j]+v[k];
                        if s-sum>0 then
                                begin
                                g:=gasit(s-sum,1,x[0],x);
                                if g<>0 then
                                        begin
                                        writeln(v[i],' ',v[j],' ',v[k],' ',v[p1[g]],' ',v[p2[g]],' ',v[p3[g]]);
                                        close(output);
                                        halt;
                                        end;
                                end;
                        end;
writeln(-1);
close(input);close(output);
end.