Pagini recente » Cod sursa (job #373675) | Cod sursa (job #1199021) | Cod sursa (job #2549426) | Cod sursa (job #313353) | Cod sursa (job #1248750)
#include<cstdio>
#include<cstdlib>
#include<cassert>
FILE *fi,*fo;
const int MAX_N = 20004;
const int MAX_G = 75004;
const int INF = 123456;
bool este[MAX_G];
int last[MAX_G];
int nr[MAX_G];
int nr_ob[205];
int sol,x,G;
int i,j,k,N;
int suma_actuala;
int main(){
assert( freopen("ghiozdan.in","r",stdin) != NULL );
assert( freopen("ghiozdan.out","w",stdout) != NULL );
assert( scanf("%d%d\n", &N, &G) == 2 );
for(i=1;i<=N;i++){
assert( scanf("%d", &x) == 1);
nr_ob[x]++;
}
for(j=1;j<=G;j++) nr[j]=INF;
este[0]=1;
last[0]=0;
nr[0]=0;
for(i=200;i>0;--i)
if(nr_ob[i]>0)
for(j=G-i;j>=0;j--)
if(este[j])
for(k=1;k<=nr_ob[i] && j+i*k<=G && nr[j]+k<nr[j+i*k];k++)
{
suma_actuala=j+i*k;
este[suma_actuala]=1;
last[suma_actuala]=i;
nr[suma_actuala]=nr[j]+k;
if(suma_actuala>sol) sol=suma_actuala;
}
assert( printf("%d %d\n", sol, nr[sol]) > 0 );
for(j=sol;j>=0;--j)
if(nr[j]+1==nr[sol] && nr_ob[sol-j])
{
--nr_ob[sol-j];
assert( printf("%d\n", sol-j) > 0 );
sol=j;
}
assert( fclose(stdin) != EOF );
assert( fclose(stdout) != EOF );
return 0;
}