Pagini recente » Cod sursa (job #2327457) | Cod sursa (job #864378) | Cod sursa (job #2349245) | Cod sursa (job #1786164) | Cod sursa (job #3278989)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, Gmax, greutate[5005]{}, profit[5005]{}, dp[5005][10005]{}, x;
int main(){
fin >> n >> Gmax;
for(int i = 1; i <= n; i++)
fin >> greutate[i] >> profit[i];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= Gmax; j++){
if(greutate[i] > j)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = max(dp[i-1][j], dp[i-1][j-greutate[i]] + profit[i]);
}
}
fout << dp[n][Gmax];
}