Cod sursa(job #1596985)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 11 februarie 2016 16:16:03
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

const int dmax = 5002;
const int G_max = 10002;

struct RUCSAC {int g, p;};
RUCSAC v[dmax];

int profit[G_max]; // profit[i] == PROFITUL MAX CE SE POATE OBTINE CU OBIECTE DE GREUTATE i

int N, K;

int main()
{
    int i, j;

    in >> N >> K;

    for(i = 1; i <= N; i++) in >> v[i].g >> v[i].p;

    for(i = 1; i <= K; i++) profit[i] = -1;

    profit[0] = 0;

    for(i = 1; i <= N; i++)
        for(j = K; j >= v[i].g; j--) // PARCURG OBIECTELE O SINGURA DATA

            if( profit[ j - v[i].g ] != -1 &&  profit[j] < profit[ j - v[i].g ] + v[i].p)
                profit[j] = profit[ j - v[i].g ] + v[i].p;

    out << profit[K];

    return 0;
}