Cod sursa(job #2121559)

Utilizator Alex18maiAlex Enache Alex18mai Data 3 februarie 2018 20:52:36
Problema Zebughil Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>

using namespace std;

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

const int inf = 1e9;

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

int BIT[20];

int main() {

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

    int test = 3;

    while (test--){

        int n , 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;
            int 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++){
            int cont = 0;

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

            for (int bit = 0; bit < n; bit++){
                if ((1 << bit) & now){
                    BIT[cont] = bit;
                    cont++;
                }
            }

            for (int comp = 0; comp < (1 << cont); comp++){
                int now = 0;
                for (int bit = 0; bit < cont; bit++){
                    if ((1 << bit) & comp){
                        now += (1 << BIT[bit]);
                    }
                }
                dp[mask ^ now] = min (dp[mask ^ now] , dp[mask] + dp[now]);
                //cout<<mask<<" "<<now<<" "<<(mask ^ now)<<" "<<dp[mask ^ now]<<'\n';
            }
        }

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

    }
    
    return 0;
}