Cod sursa(job #2589007)

Utilizator rares9991Matisan Rares-Stefan rares9991 Data 25 martie 2020 17:20:13
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

struct loto
{
    int a, b, c, sum;
}d[1000001];

int v[101], ct, n, s;

bool cmp(loto x, loto y)
{
    return x.sum<y.sum;
}

int main()
{
    in>>n>>s;
    for(int i=1; i<=n; i++)
        in>>v[i];
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        for(int k=1; k<=n; k++)
    {
        d[++ct].a=v[i];
        d[ct].b=v[j];
        d[ct].c=v[k];
        d[ct].sum=v[i]+v[j]+v[k];
    }
    sort(d+1, d+ct+1, cmp);
    int x=1, y=ct, gasit=0;
    while(x<=y)
    {
        if(d[x].sum+d[y].sum==s)
        {
            gasit=1;
            out<<d[x].a<<" "<<d[x].b<<" "<<d[x].c<<" "<<d[y].a<<" "<<d[y].b<<" "<<d[y].c;
            return 0;
        }
        else if(d[x].sum+d[y].sum<s)
            x++;
        else
            y--;
    }
    if(!gasit)
        out<<-1;
    return 0;
}