Cod sursa(job #2755740)

Utilizator lahayonTester lahayon Data 28 mai 2021 00:46:19
Problema Loto Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <vector>
#include <unordered_map>
#include <algorithm>


using namespace std;

vector<int> values;
int N, S;
    ifstream cin("loto.in");
    ofstream cout("loto.out");

bool checksol(){

    unordered_map<int, vector<int>> hashmap;
    unordered_map<int, vector<int>>::iterator it;


    for(int i = 0; i < N; ++i)
        for(int j = 0; j < N; ++j)
            for(int k = 0; k < N; ++k){

                int curr_sum = values[i] + values[j] + values[k];
                if(curr_sum < S) 
                {
                    hashmap.insert(pair<int, vector<int>>(curr_sum, {i, j, k}));

                    it = hashmap.find(S - curr_sum);
                    if(it != hashmap.end()){
                        cout << values[i] << " " << values[j] << " " << values[k] << " " << values[it->second[0]] << " " << values[it->second[1]] << " " << values[it->second[2]];
                        return true;
                    } 
                }
                else return false;
            }
    return false;
}

int main()
{ 



    cin >> N >> S;
    values.reserve(N);


    for(int i = 0; i < N; ++i)
        cin >> values[i];
        
    sort(values.begin(), values.end());

    bool amsol = checksol();
    if(!amsol)
        cout << -1;

  
    

    cin.close();
    cout.close();

  return 0;
}