Cod sursa(job #3298478)

Utilizator KapetaneAvram Armand-Florin Kapetane Data 30 mai 2025 14:45:11
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct obiect 
{
    int greutate, profit;
}v[5000];

int dp[10000]={0};
int main()
{
    int n, G;
    fin>>n>>G;
    for(int i=0;i<n;i++)
    {
        fin>>v[i].greutate>>v[i].profit;
    }
    for(int i=0;i<n;i++)
    {
        for(int g=G;g>=v[i].greutate;g--)
        {
            if(dp[g]<dp[g-v[i].greutate]+v[i].profit)
            {
                dp[g]=dp[g-v[i].greutate]+v[i].profit;
            }
        }
    }
    int Pmax = 0;
    for(int g=0;g<=G;g++)
    {
        if(dp[g]>Pmax)
            Pmax=dp[g];
    }
    
    fout<<Pmax<<endl;
    return 0;
}