Cod sursa(job #3132137)

Utilizator fanevodaCalota Stefan fanevoda Data 21 mai 2023 23:31:52
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <iostream>
#include <algorithm>

std::ifstream in("loto.in");
std::ofstream out("loto.out");


struct haz
{
    int sum, x, y, z;
}w[400001];
int hazz(haz x, haz y)
{
    return x.sum < y.sum;
}

int n, s, o[101], nr;

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

int main()
{
    read();

    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
            for (int k = j; k <= n; k++)
            {
                w[++nr].sum = o[i] + o[j] + o[k];
                w[nr].x = o[i];
                w[nr].y = o[j];
                w[nr].z = o[k];
            }
    std::sort(w + 1, w + nr + 1, hazz);

    int st = 1, dr = nr;

    while (st <= dr)
    {
        if (w[st].sum + w[dr].sum > s)
            dr--;
        else
            if (w[st].sum + w[dr].sum < s)
                st++;
            else break;
    }
    if (st > dr)
        out << -1;
    else
        out << w[st].x << " " << w[st].y << " " << w[st].z << " " << w[dr].x << " " << w[dr].y << " " << w[dr].z;
}