Cod sursa(job #1542504)

Utilizator aaether14Dinescu Stefan Cristian aaether14 Data 5 decembrie 2015 14:02:30
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#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;


}