Pagini recente » Cod sursa (job #2284612) | Cod sursa (job #2230566) | Cod sursa (job #1634718) | Cod sursa (job #2220262) | Cod sursa (job #2854440)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int N, G, x, y;
vector<int> W, P;
int dp[5001][10001];
ifstream cit("rucsac.in");
ofstream afis("rucsac.out");
int main()
{
cit>>N>>G;
for(int i = 0; i < N; i++){
cit>>x>>y;
W.push_back(x);
P.push_back(y);
}
for(int i = 0; i < N; i++){
for(int j = 1; j <= G; j++){
if(i == 0){
if(j >= W[i]){
dp[i][j] = P[i];
}
else{
dp[i][j] = 0;
}
}
else{
dp[i][j] = max(dp[i-1][j], dp[i-1][j-W[i]] + P[i]);
}
}
}
afis<<dp[N-1][G];
cit.close();
afis.close();
return 0;
}