Cod sursa(job #483739)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 9 septembrie 2010 22:20:54
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <cstdio>
#include <cstring>

#define file_in "zebughil.in"
#define file_out "zebughil.out"

#define nmax 18

int T;
int N,G;
int best[1<<nmax][nmax];
int V[nmax];

void adfile(void){
	 
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	return ;
	
}

inline int max(int a, int b) { return a>=b?a:b; }

void solve(void){
	
	int i,j,k,lim;
	
	for (T=1;T<=3;++T){
		
		scanf("%d %d", &N, &G);
		for (i=1;i<=N;++i)
			 scanf("%d", &V[i]);
		
		lim=(1<<N)-1;
		for (i=0;i<=lim;++i)
			 for (j=0;j<=N;++j)
				  best[i][j]=-1;
		best[0][0]=0;
		//O(2^N*N^2);
		
		for (i=0;i<=lim;++i)
			 for (j=0;j<=N;++j) 
				  if (best[i][j]>=0)
				      for (k=1;k<=N;++k)
						   if (!(i&(1<<(k-1))))
						   {
							   if (best[i][j]>=V[k])
								   best[(i|(1<<(k-1)))][j]=max(best[(i|(1<<(k-1)))][j],best[i][j]-V[k]);
							   else
								   best[(i|(1<<(k-1)))][j+1]=max(best[(i|(1<<(k-1)))][j+1],G-V[k]); 
						   }
		int poz=-1;
		for (i=1;i<=N && poz==-1;++i)
			 if (best[lim][i]!=-1)
				 poz=i;
		printf("%d\n", poz);
    }
}

int main(){
	
	adfile();
	solve();
	
	return 0;
	
}