Pagini recente » Cod sursa (job #541889) | Cod sursa (job #2867121) | Cod sursa (job #1246720) | Cod sursa (job #2117140) | Cod sursa (job #3320346)
#include <fstream>
#include <vector>
using namespace std;
struct obiect
{
int g, p;
};
int main()
{
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int n, k;
in >> n >> k;
vector <obiect> v(n);
vector <int> profit(k + 1, -1);
for (auto &ob: v)
{
in >> ob.g >> ob.p;
}
profit[0] = 0;
for (auto ob: v)
{
for (int j = k; j >= ob.g; j--)
{
if (profit[j-ob.g] != -1)
{
profit[j] = max(profit[j], profit[j-ob.g] + ob.p);
}
}
}
int profit_max = -1;
for (auto p: profit)
{
profit_max = max(profit_max, p);
}
out << profit_max << "\n";
in.close();
out.close();
return 0;
}