# Cod sursa(job #443905)

Utilizator Data 18 aprilie 2010 20:24:58 Loto 95 cpp done Arhiva de probleme 1.59 kb
``````//infoarena
//sliceskullcandy

#include <stdio.h>

long i,j,k,n,nv,A[102],v[1000002],ok,S;

void QuickSort(long L,long R)
{
long i,j,aux,piv;
piv=v[(L+R)/2];
i=L;
j=R;
while(i<=j)
{
while(v[i]<piv) i++;
while(piv<v[j]) j--;
if(i<=j)
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
i++;
j--;
}
}
if(i<R) QuickSort(i,R);
if(L<j) QuickSort(L,j);
}

long BS(long x,long L,long R)
{
long m=(L+R)/2;
if(L<R)
{
if(x<v[m]) return BS(x,L,m);
else
if(v[m]<x) return BS(x,m+1,R);
else return 1;
}
else return 0;
}

void ploto(long sum)
{
long i,j,k,ok;

freopen("loto.out","w",stdout);

ok=1;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
for(k=j;k<=n;k++)
if(A[i]+A[j]+A[k]==sum)
{
ok=0;
printf("%ld %ld %ld ",A[i],A[j],A[k]);
break;
}
if(ok==0) break;
}
if(ok==0) break;
}

ok=1;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
for(k=j;k<=n;k++)
if(A[i]+A[j]+A[k]==S-sum)
{
ok=0;
printf("%ld %ld %ld\n",A[i],A[j],A[k]);
break;
}
if(ok==0) break;
}
if(ok==0) break;
}
}

int main()
{
freopen("loto.in","r",stdin);

scanf("%ld%ld",&n,&S);
for(i=1;i<=n;i++)
scanf("%ld",&A[i]);

nv=0;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
for(k=j;k<=n;k++)
v[++nv]=A[i]+A[j]+A[k];

QuickSort(1,nv);

ok=0;
for(i=1;i<=nv;i++)
if(v[i]<S-v[i])
{
ok=BS(S-v[i],1,nv);
if(ok==1)
{
ploto(v[i]);
return 0;
}
}
else break;

freopen("loto.out","w",stdout);
printf("-1\n");

return 0;
}
``````