Cod sursa(job #3160145)

Utilizator DnDavid17Craciun Stefan-David DnDavid17 Data 23 octombrie 2023 09:18:39
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int max_n=5001;
const int max_g=10001;
struct t_object
{
    int weight;
    int profit;
}obj[max_n];
int dp_profit(int idx,int capacitate,t_object obj[],int memo[][max_g])
{
    if(idx==0)
        return 0;
    if(capacitate==0)
        return 0;
        if(memo[idx][capacitate]!=0)
            return memo[idx][capacitate];
    t_object curr_obj=obj[idx];
    if(curr_obj.weight>capacitate)
    {
        memo[idx][capacitate]=dp_profit(idx-1,capacitate,obj,memo);
        return memo[idx][capacitate];
    }
    int profit1=dp_profit(idx-1,capacitate,obj,memo);
    int profit2=curr_obj.profit+dp_profit(idx-1,capacitate-curr_obj.weight,obj,memo);
    return max(profit1,profit2);
}
int main()
{
    int n,greutate;
    fin>>n>>greutate;
    for(int i=1;i<=n;i++)
    {
        int curr_greutate;
        int curr_profit;
        fin>>curr_greutate>>curr_profit;
        t_object obiect={curr_greutate,curr_profit};
        obj[i]=obiect;
    }
    int memo[max_n][max_g]={0};
    fout<<dp_profit(n,greutate,obj,memo);
    return 0;
}