Pagini recente » Cod sursa (job #465063) | Cod sursa (job #155377) | Cod sursa (job #1708770) | Cod sursa (job #1506660) | Cod sursa (job #2956453)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct item
{
int weight;
int cost;
};
int Run(vector<item> items, int maximumWeight)
{
vector<int> dp(maximumWeight + 1, 0);
vector<int> dp2(maximumWeight + 1, 0);
for (auto item : items)
{
for (int weight = 0; weight <= maximumWeight; weight++)
{
if (item.weight <= weight)
{
dp2[weight] = max(dp[weight], dp[weight - item.weight] + item.cost);
}
}
dp = dp2;
}
return dp[maximumWeight];
}
int main()
{
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
int n, maximumWeight;
cin >> n >> maximumWeight;
vector<item>items;
item item;
while (cin >> item.weight >> item.cost)
{
items.push_back(item);
}
cout << Run(items, maximumWeight);
return 0;
}