Cod sursa(job #3172797)

Utilizator eugenioMarinescu Eugenio eugenio Data 21 noiembrie 2023 11:20:35
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include  <iostream>
#include <cstring>
#include <climits>
#include <bitset>
#define inf 10000000
#define upd 51
using namespace std;
ifstream cin("zebughil.in");
ofstream cout("zebughil.out");

long long n,m;
long long v[18];
long long d[(1<<16)+5][18];
bitset<16> num;

bool aflare_bit(int nr, int poz)
{
    num=nr;
    return num[poz-1];
}


int main()
{
    for(int i=1; i<=3; i++)
    {
        cin>>n>>m;
        for(int i=0; i<=(1<<n); i++)
            for(int j=1; j<=n; j++)
                d[i][j]=inf;

        for(int i=0; i<n; i++)
            cin>>v[i],d[(1<<i)][1]=v[i];

        for(int i=0; i<(1<<n); i++)
        {
            for(int j=1; j<n; j++)
                if(d[i][j]!=inf)
                    for(int x=0; x<n; x++)
                        if(!aflare_bit(d[i][j],x))
                        {
                            if(d[i][j] + v[x] <= m)
                                d[i+(1<<x)][j] = min(d[i+(1<<x)][j], d[i][j]+v[x]);
                            d[i+(1<<x)][j+1] = min(d[i+(1<<x)][j+1], v[x]);
                        }
        }

        for(int i=1; i<=n; i++)
        {
            if(d[(1<<n)-1][i]!=inf)
            {
                cout<<i<<'\n';
                return;
            }
        }
    }
    return 0;
}