Pagini recente » Monitorul de evaluare | Cod sursa (job #2850352)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int nmax = 55;
const int wmax = 105;
int dp[nmax][wmax];
struct object {
int weight;
int value;
};
int main() {
int N, maxWeight;
fin >> N >> maxWeight;
vector<object> objects(N + 1);
for(int i = 1; i <= N; ++i) {
fin >> objects[i].weight;
fin >> objects[i].value;
}
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= maxWeight; ++j)
if(j >= objects[i].weight)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - objects[i].weight] + objects[i].value);
else dp[i][j] = dp[i - 1][j];
fout << dp[N][maxWeight] << '\n';
return 0;
}