Cod sursa(job #1426502)

Utilizator greenadexIulia Harasim greenadex Data 29 aprilie 2015 20:11:55
Problema Loto Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <unordered_map>
#include <vector>

using namespace std;

ifstream fin("loto.in");
ofstream fout("loto.out");

unordered_map <int, int> sums;
vector <int> numbers;

int N, S;

int main()
{
    fin >> N >> S;
    for (int i = 1, nr; i <= N; i++){
        fin >> nr;
        numbers.push_back(nr);
    }
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            for (int l = 1; l <= N; l++){
                int sum = numbers[i] + numbers[j] + numbers[l];
                if (sum <= S)
                    sums[sum] = 1;
            }

    for (auto x : numbers)
        for (auto y : numbers)
            for (auto z : numbers){
                int sum = x + y + z;
                if (sums.count(sum) && sums.count(S - sum)){
                    for (auto a : numbers)
                    for (auto b : numbers)
                    for (auto c : numbers)
                    if (a + b + c == S - sum){
                        fout << x << ' ' << y << ' ' << z << ' '
                             << a << ' ' << b << ' ' << c;
                        return 0;
                    }
                }
            }
    fout << -1;
    return 0;
}