Pagini recente » Cod sursa (job #1016684) | Cod sursa (job #1582632) | Cod sursa (job #913156) | Cod sursa (job #2812774) | Cod sursa (job #3325705)
#include<fstream>
#include<vector>
#define INF 999999999999
#define ll long long
std::ifstream fin("zebughil.in");
std::ofstream fout("zebughil.out");
const int NMAX=19;
std::vector<std::pair<ll, ll>> dp(1<<NMAX);
void solve()
{
dp=std::vector<std::pair<ll, ll>>(1<<NMAX, {INF, 0});
int n, g;
fin>>n>>g;
std::vector<int>v(n);
for(int i=0; i<n; ++i)
fin>>v[i];
dp[0]={1, 0};
for(int msk=0; msk<(1<<n); ++msk)
{
for(int bit=0; bit<n; ++bit)
{
if(!(msk&(1<<bit)))
{
int newMask=msk|(1<<bit);
ll cntCand=dp[msk].first, newSum=dp[msk].second;
if(dp[msk].second+v[bit]>g)
{
++cntCand;
newSum=v[bit];
}
else
newSum+=v[bit];
if(cntCand<dp[newMask].first || (cntCand==dp[newMask].first && newSum<dp[newMask].second))
dp[newMask]={cntCand, newSum};
}
}
}
fout<<dp[(1<<n)-1].first<<'\n';
}
int main()
{
int t=3;
while(t--)
solve();
return 0;
}