Cod sursa(job #2121575)

Utilizator Alex18maiAlex Enache Alex18mai Data 3 februarie 2018 21:17:27
Problema Zebughil Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>

using namespace std;

ifstream cin ("zebughil.in");
ofstream cout ("zebughil.out");

const int inf = 1e9;

long long val[20];
int dp[1 << 17];

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int test = 3;

    while (test--){

        int n;
        long long g;
        cin>>n>>g;

        for (int i=1; i<=n; i++){
            cin>>val[i];
        }

        for (int mask = 0; mask < (1 << n); mask++){
            dp[mask] = inf;
            long long sum = 0;
            for (int bit = 0; bit < n; bit++){
                if ((1 << bit) & mask){
                    sum += val[bit + 1];
                }
            }
            if (sum <= g){
                dp[mask] = 1;
            }
        }

        for (int mask = 0; mask < (1 << n); mask++){

            if (mask == inf){
                continue;
            }

            int now = (1 << n) - 1;
            now ^= mask;

            int old = now;

            while (now > 0){

                dp[mask ^ now] = min (dp[mask ^ now] , dp[mask] + dp[now]);
                now --;
                now &= old;
            }

        }

        cout<<dp[(1 << n) - 1]<<'\n';

    }

    return 0;
}