Pagini recente » Cod sursa (job #750258) | Cod sursa (job #2813643) | Cod sursa (job #1548746) | Cod sursa (job #396964) | Cod sursa (job #3278987)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, Gmax, greutate[5001]{}, profit[10001]{}, dp[5001][10001]{}, 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];
}