Pagini recente » Cod sursa (job #3268526) | Cod sursa (job #357216) | Cod sursa (job #2810159) | Cod sursa (job #1416181) | Cod sursa (job #1542504)
#include <iostream>
#include <fstream>
#define N_SIZE 5001
#define G_SIZE 10001
using namespace::std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int obiecte[2][N_SIZE];
int profit[G_SIZE];
int main()
{
int n, k;
fin>>n>>k;
for(int i = 1; i <= n; i++)
fin>>obiecte[0][i]>>obiecte[1][i];
for(int j = 1; j <= k; j++)
profit[j] = -1;
profit[0] = 0;
for (int i = 1; i <= n; i++)
for (int j = k; j >= obiecte[0][i]; j--)
if (profit[j - obiecte[0][i]] != -1 && profit[j - obiecte[0][i]] + obiecte[1][i] > profit[j])
profit[j] = profit[j - obiecte[0][i]] + obiecte[1][i];
int max_profit = profit[0];
for (int i = 1; i <= k; i++)
if (max_profit < profit[i])
max_profit = profit[i];
fout<<max_profit;
return 0;
}