Cod sursa(job #2283268)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 15 noiembrie 2018 11:57:44
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream in("zebughil.in");
    ofstream out("zebughil.out");
    int t = 3;
    while(t--)
    {
        int n, g;
        in >> n >> g;
        vector<long long> v(n);
        for(int i = 0; i < n; ++i)
            in >> v[i];
        vector<int> dp(1 << n);
        for(int state = 0; state < (1 << n); ++state)
        {
            long long sum = 0;
            for(int i = 0; i < n; ++i)
                if((state & (1 << i)) != 0)
                    sum += v[i];
            if(sum <= g)
                dp[state] = 1;
            else
            {
                dp[state] = 1 << 29;
                int x;
                for(int sub = (state - 1) & state; sub > 0; sub = (sub - 1) & state)
                {
                    x = dp[sub] + dp[state - sub];
                    if(x < dp[state])
                        dp[state] = x;
                }
            }
        }
        out << dp[(1 << n) - 1] << "\n";
    }
    in.close();
    out.close();
    return 0;
}