Pagini recente » Cod sursa (job #2827256) | Cod sursa (job #2820650) | Cod sursa (job #2447834) | Cod sursa (job #148136) | Cod sursa (job #2295334)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fi("zebughil.in");
ofstream fo("zebughil.out");
const int NMAX=20;
int n,g,sum,aux,cost,prev_conf,z[NMAX];
pair <int,int> dp[(1<<NMAX)];
int main()
{
for(int t=1;t<=3;t++)
{
fi>>n>>g;
for(int i=0;i<n;i++)
fi>>z[i];
for(int conf=0;conf<=(1<<n)-1;conf++)
{
sum=0;
for(int i=0;(1<<i)<=conf;i++)
sum+=z[i];
if(sum<=g)
dp[conf]={1,sum};
else
{
dp[conf]={18,0};
for(int i=0;(1<<i)<=conf;i++)
if((1<<i)&conf)
{
prev_conf=conf-(1<<i);
cost=z[i]+dp[prev_conf].second;
aux=dp[prev_conf].first;
if(cost>g) cost=z[i],aux++;
if(dp[conf].first==aux && cost<dp[conf].second || dp[conf].first>aux)
{
dp[conf].first=aux;
dp[conf].second=cost;
}
}
}
}
fo<<dp[(1<<n)-1].first<<"\n";
}
fi.close();
fo.close();
return 0;
}