Cod sursa(job #2980530)

Utilizator IanisBelu Ianis Ianis Data 16 februarie 2023 16:41:47
Problema Loto Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>

using namespace std;

#ifdef LOCAL
ifstream fin("input.txt");
#define fout cout
#else
ifstream fin("loto.in");
ofstream fout("loto.out");
#define endl '\n'
#endif

const int NMAX = 105;
const int N3MAX = NMAX * NMAX * NMAX;

int n, s;
int a[NMAX];
int s3[N3MAX];
int8_t pos[N3MAX][3];
vector<int> ans;

void read() {
    fin >> n >> s;
    for (int i = 1; i <= n; i++)
        fin >> a[i];
}

void solve() {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                s3[++cnt] = a[i] + a[j] + a[k];
                pos[cnt][0] = i, pos[cnt][1] = j, pos[cnt][2] = k;
            }
        }
    }
    sort(s3 + 1, s3 + cnt + 1);
    for (int i = 1; i <= cnt; i++) {
        int p = lower_bound(s3 + 1, s3 + cnt + 1, s - s3[i]) - s3;
        if (s3[p] == s - s3[i]) {
            for (int j = 0; j < 3; j++) {
                ans.push_back(pos[i][j]);
                ans.push_back(pos[p][j]);
            }
            sort(ans.begin(), ans.end());
            for (auto &it : ans) {
                fout << a[it] << ' ';
            }
            return;
        }
    }
    fout << -1;
}

int main() {
    read();
    solve();
    return 0;
}