Cod sursa(job #3276461)

Utilizator tudor_costinCostin Tudor tudor_costin Data 13 februarie 2025 18:27:13
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 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=n+1;
    for(int mask=0;mask<(1<<n);mask++)
    {
        for(int j=1;j<=n;j++)
        {
           if(dp[mask][j]!=-1)
           {
               if(mask==(1<<n)-1) ans=min(ans,j);
               for(int bit=0;bit<n;bit++)
               {
                   if((bit&(1<<mask))==0)
                   {
                       int nw=(mask^(1<<bit));
                       if(a[bit]<=dp[mask][j]) dp[nw][j]=max(dp[nw][j],dp[mask][j]-a[bit]);
                       else dp[nw][j+1]=max(dp[nw][j+1],g-a[bit]);
                   }
               }
           }
        }
    }
    fout<<ans<<'\n';
}
int main()
{
    int t=3;
    while(t--)
    {
        solve();
    }
    return 0;
}