Cod sursa(job #2472429)

Utilizator DanSDan Teodor Savastre DanS Data 12 octombrie 2019 12:57:09
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>

using namespace std;

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

const int N = 5001;
const int GMAX = 10001;
int n, G, profit[GMAX], p[N], g[N];

int main()
{
    in>>n>>G;
    for(int i=1; i<=n; i++)
        in>>g[i]>>p[i];

    for(int j=1; j<=G; j++)
        profit[j] = -1;

    for(int i=1; i<=n; i++)
    {
        for(int j = G-g[i]; j>=0; j--)
        {
            if(profit[j] != -1 && profit[j] + p[i] > profit[j + g[i]])
                profit[j + g[i]] = profit[j] + p[i];
        }
    }
    out<<profit[G];
    return 0;
}