Cod sursa(job #714210)

Utilizator paulbotabota paul paulbota Data 15 martie 2012 16:04:46
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include<fstream>
#define MAXN 102
#define inf "loto.in"
#define outf "loto.out"

using namespace std;

ifstream in(inf);
ofstream out(outf);

int n,s,v[MAXN],a[7],k,m;

void read()
{
	in>>n>>s;
	for(int i=1;i<=n;i++)
		in>>v[i];
}

void swap(int h[MAXN],int x,int y)
{
	int aux=h[x];
	h[x]=h[y];
	h[y]=aux;
}

void heapify(int h[MAXN],int i)
{
	int s,d,max=i;
	s=2*i;
	d=2*i+1;
	if(s<=n && h[s]>h[i])
		max=s;
	if(d<=n && h[d]>h[max])
		max=d;
	if(max!=i)
	{
		swap(v,max,i);
		heapify(h,max);
	}
}

void heapsort(int h[MAXN])
{
	int i;
	for(i=m/2;i>=1;i--)
		heapify(h,i);
	for(i=1;i<=m;i++)
	{
		swap(v,1,m);
		m--;
		heapify(h,1);
	}
}

int suma()
{
	int sum=0;
	for(int i=1;i<=6;i++)
		sum=sum+a[i];
	return sum;
}

int solve(int y)
{
	int i;	
	m=n;
	for(i=1;i<=6;i++)
		a[i]=v[y];
	if(suma()==s)
		return 1;
	i=1;
	while(i<=6)
	{
		int k=1;
		do
		{
		k++;
		a[i]=v[k];		
		}while(suma()!=s && k!=n);
		if(suma()==s)
			return 1;
		i++;
	}
	return 0;
}

int main()
{
	read();
	m=n;
	int bun=0,j=1;
	heapsort(v);
	while(bun==0 && j<=n)
	{
		bun=solve(j);
		j++;
	}	
	if(bun==1)
	{
		for(int i=1;i<=6;i++)
			out<<a[i]<<" ";
	}
	else
		out<<"-1";
	return 0;
}