Pagini recente » Cod sursa (job #2363023) | Cod sursa (job #478137) | Cod sursa (job #2043696) | Cod sursa (job #3152013) | Cod sursa (job #567195)
Cod sursa(job #567195)
#include <iostream>
#include <fstream>
using namespace std;
#define maxN 20
#define maxN2 (1 << 19)
#define inf (1 << 30)
int best[maxN2][maxN], N, G;
int x[maxN];
int main()
{
ifstream f("zebughil.in");
ofstream g("zebughil.out");
for (int i = 1; i <= 3; ++ i)
{
int sol = inf;
f >> N >> G;
for (int j = 1; j <= N; ++ j)
f >> x[j];
for (int conf = 0; conf < (1 << N); ++ conf)
for (int col = 0; col <= N; ++ col)
best[conf][col] = inf;
best[0][1] = G;
for (int conf = 0; conf < (1 << N); ++ conf)
for (int col = 0; col <= N; ++ col)
{
if (best[conf][col] == inf) continue;
for (int bit = 0; bit < N; ++ bit)
{
if ( (1 << bit) & conf ) continue;
if (best[conf][col] >= x[bit + 1])
{
best[conf + (1 << bit)][col] = best[conf][col] - x[bit + 1];
if (conf + (1 << bit) == (1 << N) - 1)
sol = min (sol, col);
}
else
{
best[conf + (1 << bit)][col + 1] = G - x[bit + 1];
if (conf + (1 << bit) == (1 << N) - 1)
sol = min (sol, col + 1);
}
}
}
g << sol << '\n';
}
f.close();
g.close();
return 0;
}