Cod sursa(job #3130250)

Utilizator florinilie324Ilie Florin Alexandru florinilie324 Data 17 mai 2023 11:07:55
Problema Loto Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<bits/stdc++.h>
using namespace std;

int N, S;
vector<int> numere;
vector<int> rez;


bool dfs(int sum, int idx, int cnt) {
    if (cnt == 6) {
        if (sum == S) {
            return true;
        }
        return false;
    }
    if (idx == N || sum + (6 - cnt) * numere[idx] > S || sum + (6 - cnt) * numere[N - 1] < S) {
        return false;
    }
    for (int i = idx; i < N; i++) {
        rez[cnt] = numere[i];
        if (dfs(sum + numere[i], i, cnt + 1)) {
            return true;
        }
    }

    return false;
}

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

    fin >> N >> S;
    numere.resize(N);
    rez.resize(6);
    for (int i = 0; i < N; i++) {
        fin >> numere[i];
    }

    sort(numere.begin(), numere.end());

    if (dfs(0, 0, 0)) {

        for (int i = 0; i < 6; i++) {
            fout << rez[i] << " ";
        }
    } else {

        fout << -1;
    }

    fin.close();
    fout.close();

    return 0;
}