Cod sursa(job #916664)

Utilizator gener.omerGener Omer gener.omer Data 16 martie 2013 19:28:41
Problema Loto Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

#define NMAX 105

int N, S, A[NMAX];

int main()
{
	freopen("loto.in", "rt", stdin);
	freopen("loto.out", "wt", stdout);

	scanf("%d %d", &N, &S);
	for(int i = 0; i < N; i++)
		scanf("%d", &A[i]);
	
	sort(A, A+N);
		
	vector<int> sums;
	for(int i = 0; i < N;++i)
		for(int j = 0; j < N; ++j)
			sums.push_back(A[i]+A[j]);
	
	sort(sums.begin(), sums.end());
	
	int sz = sums.size();
	
	for(int i = 0; i < sz; ++i)
		for(int j = 0; j < sz; j++)
		{
			int s = S - sums[i] - sums[j];
			if(binary_search(sums.begin(), sums.end(), s))
			{
				int s1, s2, s3;
				s1 = sums[i];
				s2 = sums[j];
				s3 = S - s1 - s2;
				// solution
				for(int i = 0; i < N; ++i){
					int p = s1 - A[i];
					if(binary_search(A, A+N, p)){
						printf("%d %d ", A[i] , p);
						break;
					}
				}
				
				for(int i = 0; i < N; ++i){
					int p = s2 - A[i];
					if(binary_search(A, A+N, p)){
						printf("%d %d ", A[i] , p);
						break;
					}
				}
				
				for(int i = 0; i < N; ++i){
					int p = s3 - A[i];
					if(binary_search(A, A+N, p)){
						printf("%d %d ", A[i] , p);
						break;
					}
				}
				return 0;
			}
		}
		
	printf("%d", -1);
	
	return 0;
}