Cod sursa(job #2836493)

Utilizator TeoRoGaming_YgVoinea Ionut-Florin TeoRoGaming_Yg Data 20 ianuarie 2022 15:29:22
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>

using namespace std;

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

const int N=5000;
const int G=10000;

int profit[G+1],a[N+1],b[N+1];

int main()
{
    int n, k, maxx=0, i, j;
    f>>n>>k;
    for(i=0; i<n; i++)
    {
        f>>a[i]>>b[i];
    }
    for(j=1; j<=k; j++)
    {
        profit[j]=-1;
    }
    for(i=0; i<n; i++)
    {
        for(j=k-a[i]; j>=0; j--)
        {
            if(profit[j]!=-1)
            {
                profit[j+a[i]]=max(profit[j]+b[i], profit[j+a[i]]);
            }
        }
    }
    for(i=1; i<=k; i++)
    {
        if(profit[i]>maxx)
            maxx=profit[i];
    }
    g<<maxx;
    return 0;
}