Cod sursa(job #1853459)

Utilizator IlluminatehPinzariu Denis Stefan Illuminateh Data 21 ianuarie 2017 20:03:12
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>

using namespace std;

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


/*



prof[j] = profitul maxim care poate fi obtinut cu obiecte care au suma greutatilor j (1<= j <= k, k = capacitate)

0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16

0 -1 -1 -1 -1 -1 -1 -1 -1 -1  -1  -1  -1  -1  -1  -1  -1

--------------------------------------------------------------
g[i]

0  1  2  3  4  5  6

x  3  3  1  1  2  1

--------------------------------------------------------------
p[i]

0  1  2  3  4  5  6

x  7  4  2  9  4  5

--------------------------------------------------------------
k = 10
*/


int main()
{
    int N, k, i, j, p[5000], g[5000], prof[10000];

    in>>N>>k;

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

    /*
    for(j = 1; j<=N; j++)
    {
        cout<<p[j]<<' ';
    }
    cout<<endl<<endl;

    for(j = 1; j<=N; j++)
    {
        cout<<g[j]<<' ';
    }
    cout<<endl<<endl;

    */


    for(j = 0; j<=k+20; j++)
        prof[j] = -1;

    prof[0] = 0;

    for(int i=1; i<=N; i++)
    {
        for(j = k-g[i]; j>=0; j--)
        {
            if(prof[j] != -1 && prof[j] + p[i] > prof[j+g[i]] )
                prof[j+g[i]] = prof[j] + p[i];

        }

    }


    while(prof[k] == -1)
        k--;

    out<<prof[k];
    return 0;
}