Cod sursa(job #1810610)

Utilizator silkMarin Dragos silk Data 20 noiembrie 2016 12:44:55
Problema Zebughil Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <cstdio>
#include <algorithm>
#define NMax 17
using namespace std;

char sol[NMax+1];
char is[NMax+1];
char a[NMax+1];
int v[NMax+1];

int main(){
    freopen("zebughil.in","r",stdin);
    freopen("zebughil.out","w",stdout);

    int i,j,ans,rest,maxim,ok,T,N,G;
    long long temp;

    T = 3;
    while(T--)
    {
        scanf("%d %d",&N,&G);
        for(ans = 0, i = 1; i <= N; ++i) scanf("%d",&v[i]);
        sort(v+1,v+N+1);

        for(i = 1; i <= N; ++i) sol[i] = is[i] = 0;
        for(i = N; i >= 1; --i)
        if( !is[i] )
        {
            ++ans;
            rest = G-v[i];
            is[i] = 1;
            maxim = -1;
            while(1)
            {
                for(temp = 0, j = 1; j <= N; ++j)
                if( a[j] ) temp += v[j];

                for(ok = 1, j = 1; j <= N; ++j)
                if( a[j] && is[j] ) { ok = 0; break; }

                if( temp > maxim && temp <= rest && ok )
                {
                    maxim = temp;
                    for(j = 1; j <= N; ++j) sol[j] = a[j];
                }

                for(j = N; j >= 1 && a[j]; --j) a[j] = 0;
                if(!j) break;
                a[j] = 1;
            }

            for(j = 1; j <= N; ++j)
            if( sol[j] ) is[j] = 1;
        }

        printf("%d\n",ans);
    }




return 0;
}