Cod sursa(job #69447)

Utilizator zobicaMarin Marin zobica Data 3 iulie 2007 01:06:38
Problema Loto Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include<fstream>

using namespace std;

long a[100],T;
ofstream fout("loto.out");

short n;

void citire(){
	ifstream fin("loto.in");
	fin>>n>>T;
	for (short i=0;i<n;i++)
		fin>>a[i];
	fin.close();
}

void bule(){
	short ok=0,p=n;
	while(ok==0){
		ok=1;
		p--;
		for (short i=0;i<p;i++)
			if (a[i] < a[i+1]){
				long aux=a[i];
				a[i]=a[i+1];
				a[i+1]=aux;
				ok=0;}
	}
}
int caut(long x, int o){
	int st = o, dr = n - 1;
	while (st<=dr){
		int m=(st+dr)/2;
		if (a[m] == x)
			return 1;
		if (a[m]<x)
			st=m+1;
		dr=m-1;
	}
	return 0;
}

int parc(){
	for (short i = 0; i < n; i++) {
		long s = a[i];
		if (s > T)
			continue;
		for (short j = i; j < n; j++) {
			s = a[i] + a[j];
			if (s > T)
				continue;
			for (short k = j; k < n; k++) {
				s = a[i] + a[j] + a[k];
				if (s > T)
					continue;
				for (short l = k; l < n; l++) {
					s = a[i] + a[j] + a[k] + a[l];
					if (s > T)
						continue;
					for (short o = l; o < n; o++) {
						long x = T - a[i] - a[j] - a[k] - a[l] - a[o];
						if (T >= 0)
							for (short p = o ; p < n; p++)
								if ( x == a[p]){
									fout<<a[i]<<" "<<a[j]<<" "<<a[k]<<" "<<a[l]<<" "<<a[o]<<" "<<x<<"\n";
									return 1;
								}
					}
				}
			}
		}

	}
	return 0;
}

int main(){
	citire();
	bule();	 
	if (a[n-1] > T || !parc())
		fout<<"-1";
	fout.close();
	return 0;
}