Cod sursa(job #1596987)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 11 februarie 2016 16:20:11
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 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 Max;

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;

                 if(Max < profit[j]) Max = profit[j];
            }


    out << Max;

    return 0;
}