Cod sursa(job #2470545)
| Utilizator | Data | 9 octombrie 2019 14:35:02 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include <iostream>
using namespace std;
int profit[1000000], w[10000], p[10000];
int main()
{
int n, g;
cin >> n >> g;
for(int i = 1; i <= g; i++){
profit[i] = -1;
}
for(int i = 0; i < n; i++){
cin >> 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;
}
