Cod sursa(job #1571675)

Utilizator BugirosRobert Bugiros Data 18 ianuarie 2016 12:39:24
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
using namespace std;

const int MAXG = 10005;

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

int profit[MAXG];

int g;

int main()
{
    int n;
    in >> n >> g;
    for (int i = 0;i <= g;++i)
        profit[i] = -1;
    int valoare,greutate;
    for (int obiect = 1;obiect <= n;++obiect)
    {
        in >> greutate >> valoare;
        for (int i = g - greutate;i >= 0;--i)
            if (profit[i] != -1 && profit[i + greutate] < profit[i] + valoare)
                profit[i + greutate] = profit[i] + valoare;
        if (valoare > profit[greutate])
            profit[greutate] = valoare;
    }

    int rasp = -1;
    for (int i = 0;i <= g;++i)
        if (rasp < profit[i])
            rasp = profit[i];
    out << rasp << '\n';
    return 0;
}