Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3367227) | Cod sursa (job #3366827) | Cod sursa (job #3367228)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, maxG;
int greutate[5001];
int profit[5001];
array<int, 10001> dp1;
array<int, 10001> dp2;
int main() {
fin>>n>>maxG;
for(int i = 1; i <= n; i++){
fin>>greutate[i];
fin>>profit[i];
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= maxG; j++){
if(greutate[i] > j)
dp1[j] = dp2[j];
else
dp1[j] = max(dp2[j], dp2[j - greutate[i]] + profit[i]);
}
dp2 = dp1;
}
fout<<dp1[maxG];
return 0;
}