Cod sursa(job #3276460)

Utilizator tudor_costinCostin Tudor tudor_costin Data 13 februarie 2025 18:21:27
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");
const int Nmax=18;
int dp[(1<<Nmax)][Nmax];
int a[Nmax];
void solve()
{
    int n,g;
    fin>>n>>g;
    for(int i=0;i<n;i++) fin>>a[i];
    for(int mask=0;mask<(1<<n);mask++)
    {
        for(int j=0;j<=n;j++) dp[mask][j]=-1;
    }
    dp[0][1]=g;
    int ans=0;
    for(int mask=1;mask<(1<<n);mask++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int bit=0;bit<n;bit++)
            {
                if((mask&(1<<bit))!=0)
                {
                    int prv=(mask^(1<<bit));
                    if(dp[prv][j]>=a[bit]) dp[mask][j]=max(dp[mask][j],dp[prv][j]-a[bit]);
                    else
                    {
                        if(dp[prv][j-1]!=-1) dp[mask][j]=max(dp[mask][j],g-a[bit]);
                    }
                }
            }
            ///cout<<mask<<' '<<j<<' '<<dp[mask][j]<<'\n';
            if(ans==0 && mask==((1<<n)-1))
            {
                if(dp[mask][j]!=-1) ans=j;
            }
        }
    }
    fout<<ans<<'\n';
}
int main()
{
    int t=3;
    while(t--)
    {
        solve();
    }
    return 0;
}