Cod sursa(job #633547)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 13 noiembrie 2011 23:33:42
Problema Loto Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.29 kb
Program loto;
type tip=record
    s,x,y,z:longint;
      end;
 var a:array [0..100] of longint;
    b:array [0..500000] of tip;
    b1,b2:array [1..1 shl 10] of char;
    i,j,k,n,t,g,s,l,r,mid:longint;
    fi,fo:text;
procedure sort(l,r:longint);
 var i,j,k:longint;
      y:tip;
begin
 i:=l; j:=r; k:=b[(l+r) div 2].s;
 repeat
  while k>b[i].s do inc(i);
   while k<b[j].s do dec(j);
  if i<=j then begin
              y:=b[i]; b[i]:=b[j]; b[j]:=y;
              inc(i); dec(j);
              end;
 until i>=j;
 if i<r then sort(i,r);
  if j>l then sort(l,j);
end;
begin
 assign(fi,'loto.in');
  assign(fo,'loto.out');
 settextbuf(fi,b1); settextbuf(fo,b2);
 reset(fi); rewrite(fo);
 readln(fi,n,s);
 for i:=1 to n do read(fi,a[i]);
  for i:=1 to n do
   for j:=i to n do
    for k:=j to n do
     if a[i]+a[j]+a[k]<s then begin
                      inc(t); b[t].s:=a[i]+a[j]+a[k];
                       b[t].x:=a[i]; b[t].y:=a[j]; b[t].z:=a[k];
                      end;
  sort(1,t); l:=1; r:=t;
 while (l<=r) and (b[l].s+b[j].s<>s) do if b[l].s+b[r].s>s then dec(r)
                                                            else inc(l);
 if l<=r then write(fo,b[l].x,' ',b[l].y,' ',b[l].z,' ',b[r].x,' ',b[r].y,' ',b[r].z)
          else write(fo,'-1');
close(fo);
end.