Cod sursa(job #3359131)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 24 iunie 2026 21:09:04
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;

#define USE_STD_IO 0
#if USE_STD_IO
    #define fin cin
    #define fout cout
#else
    ifstream fin("zebughil.in");
    ofstream fout("zebughil.out");
#endif

const int MOD = 1e9 + 7;
struct Dinamica {
    int drum, masa;
};
long long n, g, i, v[102];
Dinamica d[1 << 18];

static inline void Test() {
    fin >> n >> g;
    d[0] = {0, 0};
    for(i = 0; i < n; i++) fin >> v[i];

    const int lim = (1 << n);
    for(i = 1; lim > i; i++) d[i] = Dinamica{MOD, MOD};
    for(int msc = 0; lim > msc; msc++) {
        for(int i = 0; n > i; i++) {
            if(!(msc >> i & 1)) {

                Dinamica dNou = d[msc];
                if(v[i] + dNou.masa <= g) dNou.masa += v[i];
                else {
                    dNou.drum++;
                    dNou.masa = v[i];
                }

                int mscUrm = (msc | (1 << i));

                if(d[mscUrm].drum > dNou.drum) d[mscUrm] = dNou;
                else if(d[mscUrm].drum == dNou.drum && d[mscUrm].masa > dNou.masa) {
                    d[mscUrm] = dNou;
                }
            }
        }
    }
    fout << d[lim - 1].drum + (0 < d[lim - 1].masa) << '\n';
}

int main() {
    #if USE_STD_IO
        ios_base::sync_with_stdio(false);
    #endif // USE_STD_IO
    fin.tie(NULL);
    fout.tie(NULL);

    Test();
    Test();
    Test();

    return 0;
}