Cod sursa(job #2504728)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 5 decembrie 2019 14:01:00
Problema Loto Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

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

int v[105],n,S,ans,Bin[1000005],c=0;

bool ok=0;

map <int,int> M;

void Sol(int Sum)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;j<=n;k++)
            {
                if(v[i]+v[j]+v[k]==Sum)
                {
                    fout<<v[i]<<" "<<v[j]<<" "<<v[k]<<" ";
                    return;
                }
            }
        }
    }
}

int main()
{
    fin>>n>>S;
    for(int i=1;i<=n;i++)
        fin>>v[i];
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int aux=v[i]+v[j]+v[k];
                M[aux]++;
                if(M[aux]==1)
                {
                    c++;
                    Bin[c]=aux;
                }
            }
        }
    }
    sort(Bin+1,Bin+1+c);
    for(int i=1;i<=c;i++)
    {
        if(Bin[i]<=S)
        {
            int aux=S-Bin[i];
            if(binary_search(Bin+1,Bin+1+c,aux))
            {
                Sol(aux);
                Sol(Bin[i]);
                ok=1;
                break;
            }
        }
        else
            break;
    }
    if(!ok)
        fout<<-1<<'\n';
}