Cod sursa(job #907360)

Utilizator sebii_cSebastian Claici sebii_c Data 7 martie 2013 21:32:37
Problema Zebughil Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <cstdio>
#include <cstring>

#include <algorithm>

using namespace std;

const int MAXN = 18;

long long max_weight;

int N, sol;
long long weight[MAXN];
bool mark[MAXN];

bool all_marked()
{
    bool res = true;
    for (int i = 0; i < N; ++i)
        res &= mark[i];

    return res;
}

void doit(int trucks, long long w)
{
    if (all_marked()) {
        sol = min(sol, trucks);
        return;
    }

    if(w == 0)
        doit(trucks + 1, max_weight);

    bool can_take = false;
    for (int i = 0; i < N; ++i) {
        if (!mark[i]) {
            if (weight[i] > w)
                continue;
            can_take = true;
            mark[i] = true;
            doit(trucks, w - weight[i]);
            mark[i] = false;
        }
    }
    if (!can_take)
        doit(trucks + 1, max_weight);
}

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

    int t = 3;
    while (t--) {
        scanf("%d %lld", &N, &max_weight);
        for (int i = 0; i < N; ++i) {
            mark[i] = false;
            scanf("%lld", &weight[i]);
        }
        
        sol = (int)(2e9);
        doit(1, max_weight);
        printf("%d\n", sol);
    }

    return 0;
}