Cod sursa(job #2177384)

Utilizator andreiudilaUdila Andrei andreiudila Data 18 martie 2018 15:17:12
Problema Loto Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("loto.in");
ofstream cout("loto.out");
long long n,s,a[105];
int cauta(int s)
{
    int l=1,r=n;
    while(l<=r)
    {
        int mid = (l+r)/2;
        if(a[mid] == s) return mid;
        else if(a[mid] < s ) l=r+1;
        else r = mid-1;
    }

    return -1;
}
int main()
{
    cin>>n>>s;
    for(int i=1; i<=n; ++i) cin>>a[i];
    sort(a+1,a+n+1);

    for(int nr1=1; nr1<=n; ++nr1)
        for(int nr2=1; nr2<=n; ++nr2)
            for(int nr3=1; nr3<=n; ++nr3)
                for(int nr4=1; nr4<=n; ++nr4)
                    for(int nr5=1; nr5<=n; ++nr5)
                    {
                       long long sum = a[nr1]+a[nr2]+a[nr3]+a[nr4]+a[nr5];
                        int poz = cauta(s-sum);
                        if(poz != -1)
                        {
                            cout<<a[nr1]<<" "<<a[nr2]<<" "<<a[nr3]<<" "<<a[nr4]<<" "<<a[nr5]<<" "<<a[poz];
                            return 0;
                        }
                    }
        cout<<"-1";
    return 0;
}