Pagini recente » Cod sursa (job #698110) | Cod sursa (job #818272) | Cod sursa (job #37191) | Cod sursa (job #2498202) | Cod sursa (job #2603534)
#include <fstream>
#include <limits>
#include <vector>
#include <queue>
#include <tuple>
#include <iostream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
int n, m, a, b;
fin>>n>>m;
vector<int> weight(n+1);
vector<int> profit(m+1);
for (int i = 1; i <= n; ++i) {
fin>>a>>b;
weight[i] = a;
profit[i] = b;
}
vector<vector<int> > dp(n+1, vector<int>(m+1, 0));
for (int i = 1; i <= n; ++i) {
for (int cost = 0; cost <= m; ++cost) {
if (cost - weight[i] >= 0 )
dp[i][cost] = max(dp[i-1][cost], dp[i-1][cost - weight[i]] + profit[i]);
else
dp[i][cost] = dp[i-1][cost];
}
}
int ret = -1;
for (int cost = 0; cost <= m; ++cost) {
ret = max(ret, dp[n][cost]);
}
fout<<ret;
return 0;
}