Pagini recente » Cod sursa (job #3269389) | Cod sursa (job #3263488) | Ghid pentru arhiva educationala | Cod sursa (job #3121810) | Cod sursa (job #3276461)
#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;
}