Cod sursa(job #1443177)

Utilizator AndreosAndrei Otetea Andreos Data 27 mai 2015 06:55:50
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <cstdio>
#include <algorithm>
using namespace std;
struct LOTO
{
    int a,b,c,total;
};
bool cmp(LOTO x,LOTO y)
{
  return x.total<y.total;
}
LOTO rez[1000005];
int v[105];
int main()
{   freopen("loto.in","r",stdin);
    freopen("loto.out","w",stdout);
    int n,s,i,cont=0,k,j,st,dr;
    scanf("%d%d",&n,&s);
    for(i=1;i<=n;i++)
        scanf("%d",v+i);
    for(i=1;i<=n;i++)
    {
        for(j=i;j<=n;j++)
        {
            for(k=j;k<=n;k++)
            {
                cont++;
                rez[cont].a=v[i];
                rez[cont].b=v[j];
                rez[cont].c=v[k];
                rez[cont].total=rez[cont].a+rez[cont].b+rez[cont].c;
            }
        }
    }
    sort(rez+1,rez+cont+1,cmp);
    st=1;
    dr=cont;
    while(st<=dr)
    {
        if(rez[st].total+rez[dr].total==s)
            break;
        else
        {
            if(rez[st].total+rez[dr].total<s)
                st++;
            else
                dr--;
        }
    }
    if(st<=dr)
        printf("%d %d %d %d %d %d\n",rez[st].a,rez[dr].a,rez[st].b,rez[dr].b,rez[st].c,rez[dr].c);
    else
        printf("-1\n");
    return 0;
}