Pagini recente » Cod sursa (job #1239926) | Cod sursa (job #767088) | Cod sursa (job #629507) | Cod sursa (job #570505) | Cod sursa (job #2165577)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");
const int nmax = 20;
int N,G;
int a[nmax];
int dp[(1 << 18) + 5][2];
inline void Read()
{
fin >> N >> G;
for(int i = 0; i < N; i++) fin >> a[i];
cout << N << " " << G << "\n";
}
inline void Solve()
{
int stare,j,M;
M = (1 << N) - 1;
for(stare = 1; stare <= M; ++stare)
dp[stare][0] = N+1;
dp[0][1] = G; dp[0][0] = 1;
for(stare = 1; stare <= M; ++stare)
for(j = 0; j < N; j++)
{
if(stare & (1 << j))
{
if(dp[stare][0] > dp[stare ^ (1 << j)][0])
{
if (dp[stare ^ (1 << j)][1] - a[j] >= 0)
{
dp[stare][0] = dp[stare ^ (1 << j)][0];
dp[stare][1] = dp[stare ^ (1 << j)][1] - a[j];
}
}
if(dp[stare][0] > dp[stare ^ (1 << j)][0] + 1)
{
dp[stare][0] = dp[stare ^ (1 << j)][0] + 1;
dp[stare][1] = G - a[j];
}
else if(dp[stare][0] == dp[stare ^ (1 << j)][0])
dp[stare][1] = max(dp[stare][1],dp[stare ^ (1 << j)][1] - a[j]);
}
}
fout << dp[M][0] << "\n";
}
int main()
{
int T = 3;
while(T--)
{
Read();
Solve();
}
return 0;
}