Pagini recente » Cod sursa (job #3160502) | Cod sursa (job #2777455) | Cod sursa (job #1678799) | Cod sursa (job #473563) | Cod sursa (job #1810612)
#include <cstdio>
#include <algorithm>
#define NMax 17
using namespace std;
char sol[NMax+1];
char is[NMax+1];
char a[NMax+1];
int v[NMax+1];
int main(){
freopen("zebughil.in","r",stdin);
freopen("zebughil.out","w",stdout);
int i,j,x,ans,rest,maxim,ok,T,N,M,G;
long long temp;
T = 3;
while(T--)
{
scanf("%d %d",&N,&G);
for(M = ans = 0, i = 1; i <= N; ++i)
{
scanf("%d",&x);
if(x) v[++M] = x;
}
N = M;
sort(v+1,v+N+1);
for(i = 1; i <= N; ++i) a[i] = sol[i] = is[i] = 0;
for(i = N; i >= 1; --i)
if( !is[i] )
{
++ans;
rest = G-v[i];
is[i] = 1;
maxim = -1;
while(1)
{
for(temp = 0, j = 1; j <= N; ++j)
if( a[j] ) temp += v[j];
for(ok = 1, j = 1; j <= N; ++j)
if( a[j] && is[j] ) { ok = 0; break; }
if( temp > maxim && temp <= rest && ok )
{
maxim = temp;
for(j = 1; j <= N; ++j) sol[j] = a[j];
}
for(j = N; j >= 1 && a[j]; --j) a[j] = 0;
if(!j) break;
a[j] = 1;
}
for(j = 1; j <= N; ++j)
if( sol[j] ) is[j] = 1;
}
printf("%d\n",ans);
}
return 0;
}