Pagini recente » Cod sursa (job #2188728) | Cod sursa (job #1609433) | Cod sursa (job #2842189) | Cod sursa (job #2849847) | Cod sursa (job #1853433)
#include <stdio.h>
int d[10001]; ///profitu in d[i], unde i e o greutate
int maxi( int a, int b ){
return ( a>=b ) ? a : b;
}
int main()
{
int n, gmax, i, j, g, cost, max, last;
FILE *fin, *fout;
fin = fopen( "rucsac.in", "r" );
fscanf( fin, "%d%d", &n, &gmax );
d[0] = 1;
max = -1;
last = 0;
for( i=0; i<n; i++ ){
fscanf( fin, "%d%d", &cost, &g );
for( j=last; j>=0; j-- ){
if( j + cost <= gmax ){ ///daca este un cost acceptabil
d[ j+cost ] = maxi( d[j+cost], d[j] + g ); ///daca adaugand obiectul obtin un profit mai mare
last = maxi( last, j + cost ); ///ultima greutate posibila
max = maxi( max, d[ j+cost ] ); ///profitul maxim
}
}
}
fclose( fin );
fout = fopen( "rucsac.out", "w" );
fprintf( fout, "%d\n", max - 1 );
fclose( fout );
return 0;
}