Cod sursa(job #1617888)

Utilizator al.mocanuAlexandru Mocanu al.mocanu Data 27 februarie 2016 17:01:40
Problema Loto Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>
#include <algorithm>
#include <map>
#include <vector>
#define MAX 105
using namespace std;

int n, s, v[MAX];
vector<int> l;
map<int, vector<int> > m;

int main(){
	freopen("loto.in", "r", stdin);
	freopen("loto.out", "w", stdout);
	scanf("%d%d", &n, &s);
	for(int i = 0; i < n; i++)
		scanf("%d", &v[i]);
	for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			for(int k = j; k < n; k++){
				int sum = v[i] + v[j] + v[k];
				l.push_back(sum);
				vector<int> x;
				x.push_back(v[i]);
				x.push_back(v[j]);
				x.push_back(v[k]);
				m[sum] = x;
			}

	sort(l.begin(), l.end());
	int i = 0, j = l.size() - 1;
	while(i != j){
		if(l[i] + l[j] == s){
			vector<int> res;
			res.push_back(m[l[i]][0]);
			res.push_back(m[l[i]][1]);
			res.push_back(m[l[i]][2]);
			res.push_back(m[l[j]][0]);
			res.push_back(m[l[j]][1]);
			res.push_back(m[l[j]][2]);
			sort(res.begin(), res.end());
			for(int i = 0; i < 6; i++)
				printf("%d ", res[i]);
			printf("\n");
			return 0;
		}
		if(l[i] + l[j] < s)
			i++;
		else
			j--;
	}
	printf("-1\n");
	return 0;
}