Pagini recente » Cod sursa (job #2846691) | Cod sursa (job #438664) | Cod sursa (job #2029249) | Cod sursa (job #1692318) | Cod sursa (job #2470546)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int profit[1000000], w[10000], p[10000];
int main()
{
int n, g;
in >> n >> g;
for(int i = 1; i <= g; i++){
profit[i] = -1;
}
for(int i = 0; i < n; i++){
in >> w[i] >> p[i];
}
for(int i = 0; i < n; i++){
for(int j = g - w[i]; j >= 0; j--){
if(profit[j] != -1 && profit[j] + p[i] > profit[j + w[i]]){
profit[j + w[i]] = profit[j] + p[i];
}
}
}
int maxi = 0;
for(int i = 0; i <=g; i++){
maxi = max(maxi, profit[i]);
}
cout << maxi;
return 0;
}