Pagini recente » Cod sursa (job #378672) | Cod sursa (job #360462) | Cod sursa (job #1932269) | Cod sursa (job #630651) | Cod sursa (job #2970601)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
int p[5001], g[5001], profit[10001], n, k;
fin >> n >> k;
for(int i = 1; i <= n; i++){
fin >> g[i] >> p[i];
}
for(int j = 1; j<= k; j++){
profit[j] = -1;
}
profit[0] = 0;
for(int i = 0; i <= n;i++){
///actualizez vectorul profit folosind obiectul i
for(int j = k-g[i]; j >= 0; j--){
if(profit[j] != -1 && profit[j] + p[i] > j + g[i]){
profit[j + g[i]] = profit[j] + p[i];
}
}
}
fout<<profit[k];
return 0;
}