Pagini recente » Clasament infoabc_9_3 | Cod sursa (job #693638) | Cod sursa (job #1858787) | Cod sursa (job #1499517) | Cod sursa (job #2941953)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int N_MAX = 5000;
const int P_MAX = 10000;
struct rucsac
{
int greutate, profit;
};
rucsac v[N_MAX + 5];
int Profit[P_MAX + 5];
int main()
{
int n, k;
in >> n >> k;
for(int i = 0; i < n; i++)
{
in >> v[i].greutate >> v[i].profit;
}
for(int j = 1; j <= k; j++)
{
Profit[j] = -1;
}
for(int i = 0; i < n; i++)
{
for(int j = k - v[i].greutate; j >= 0; j--)
{
if(Profit[j] != -1)
{
Profit[j + v[i].greutate] = max(Profit[j + v[i].greutate], Profit[j] + v[i].profit);
}
}
}
int profit_maxim = 0;
for(int i = 1; i <= k; i++)
{
profit_maxim = max(profit_maxim, Profit[i]);
}
out << profit_maxim;
in.close();
out.close();
return 0;
}