Cod sursa(job #2452740)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 1 septembrie 2019 05:03:41
Problema Zebughil Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("zebughil.in");
ofstream out("zebughil.out");
#define int long long
const int maxn = 18;
long long dp[(1 << maxn)];
int v[maxn];

void solve()
{
    for(int i = 0; i < (1 << maxn); i++)
        dp[i] = 0;
    int n, G;
    in >> n >> G;
    for(int i = 1; i <= n; i++)
        in >> v[i];
    for(int conf = 0; conf < (1 << n); conf++)
    {
        int s = 0;
        for(int i = 0; i < n; i++)
            if(conf & (1 << i))
                s = s + v[i + 1];
        if(G >= s)
        {
            dp[conf] = 1;
            continue;
        }
        dp[conf] = (1 << 30);
        for(int ant = (conf - 1) & conf; ant > 0; ant--)
            dp[conf] = min(dp[conf], dp[ant] + dp[conf - ant]);
    }
    out << dp[(1 << n) - 1] << "\n";
    return;
}

int32_t main()
{
    int T = 3;
    for(int i = 1; i <= T; i++)
        solve();
    return 0;
}