Pagini recente » Cod sursa (job #2758464) | Cod sursa (job #1853310) | Cod sursa (job #1671526) | Cod sursa (job #909631) | Cod sursa (job #287396)
Cod sursa(job #287396)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 18;
bool dp[1<<maxn][maxn];
ll cost[maxn];
ll G;
int n;
int ares = inf;
#define isSet(mask, i) ((mask&(1<<(i))))
#define Set(mask, i) ((mask|(1<<(i))))
void read()
{
scanf("%d%lld\n", &n, &G);
for (int i = 0; i < n; ++i) scanf("%lld", &cost[i]);
}
bool solve(ll mask, ll cap, int num)
{
bool &res = dp[mask][num];
if (mask == (1<<n)-1)
{
if (num < ares) ares = num;
return 1;
}
if (res) return res;
for (int i = 0; i < n; ++i)
{
if (isSet(mask, i)) continue;
if (cost[i] > cap)
solve(Set(mask,i), G - cost[i], num + 1);
else
solve(Set(mask,i), cap - cost[i], num);
}
}
int main()
{
freopen("zebughil.in","r",stdin);
freopen("zebughil.out","w",stdout);
for (int test = 0; test < 3; ++test)
{
read();
memset(dp, false, sizeof(dp));
//if (check()) printf(;
solve(0, G, 1);
printf("%d\n", ares);
}
return 0;
}