Cod sursa(job #2468010)

Utilizator AndreeaGherghescuAndreea Gherghescu AndreeaGherghescu Data 5 octombrie 2019 11:40:04
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("rucsac.in");
ofstream out ("rucsac.out");
int w[5002],p[5002],profit[10002];
int main()
{
    int n,g;
    in>>n>>g;
    for (int i=1;i<=n;i++)
    {
        in>>w[i]>>p[i];
    }
    for (int i=1;i<=g;i++)
        profit[i]=-1;
    for (int i=1;i<=n;i++)
    {
        for (int j=g-w[i];j>=0;j--)
        {
            if (profit[j]!=-1)
            {
                if (profit[j]+p[i]>profit[j+w[i]])
                    profit[j+w[i]]=profit[j]+p[i];
            }
        }
    }
    out<<profit[g];
    return 0;
}