Cod sursa(job #3301282)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 23 iunie 2025 19:14:23
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;

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

long long n, w, i, j, v[22];
pair<long long, long long> d[(1 << 18) + 2];

static inline void Precalc() {}
static inline void Test(int testCur = 0) {
    fin >> n >> w;
    for(i = 1; i <= n; i++) fin >> v[i];
    d[0].first = 1;

    for(i = 1; i < (1 << n); i++) {
        d[i] = {n + 1, 0};
        for(j = 1; j <= n; j++) {
            if(!(i >> (j - 1) & 1)) continue;

            if(d[i - (1 << (j - 1))].second + v[j] <= w) d[i] = min(d[i], {d[i - (1 << (j - 1))].first    , v[j] + d[i - (1 << (j - 1))].second});
            else                                         d[i] = min(d[i], {d[i - (1 << (j - 1))].first + 1, v[j]});
        }
    }
    fout << d[(1 << n) - 1].first << "\n";
}

int main() {
    if(ST_DIO) ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    Precalc();
    int t = 3;
    //fin >> t;
    for(int i = 1; i <= t; i++)
        Test(i);

    return 0;
}