Pagini recente » Cod sursa (job #935113) | Cod sursa (job #114826) | Cod sursa (job #1468678) | Cod sursa (job #1661918) | Cod sursa (job #2785071)
#include<stdio.h>
#define N 5001
#define G 10001
using namespace std;
int weights[N], profits[G];
int max_profits[G];
int max(int a, int b) {
if(a > b) {
return a;
} else {
return b;
}
}
int main() {
FILE *fp1, *fp2;
fp1 = freopen("rucsac.in", "r", stdin);
fp2 = freopen("rucsac.out", "w", stdout);
int n, greutate_maxima, profit_ob_introdus, profit_ob_neintrodus;
scanf("%d %d", &n, &greutate_maxima);
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &weights[i], &profits[i]);
}
for (int i = 1; i <= n; ++i)
for(int j = G; j - weights[i] >= 0; --j) {
profit_ob_neintrodus = max_profits[j];
profit_ob_introdus = max_profits[j - weights[i]] + profits[i];
max_profits[j] = max(profit_ob_introdus, profit_ob_neintrodus);
}
printf("%d", max_profits[G]);
fclose(fp1);
fclose(fp2);
return 0;
}