Cod sursa(job #3298481)

Utilizator KapetaneAvram Armand-Florin Kapetane Data 30 mai 2025 14:54:41
Problema Problema rucsacului Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <stdio.h>
typedef struct  
{
    int greutate, profit;
}obiect;
obiect v[5000];
int dp[10000]={0};
int main()
{
    int n, G;
    FILE *fin=NULL;
    fin=fopen("rucsac.in","r");
    FILE *fout=NULL;
    fout=fopen("rucsac.out","w");
    fscanf(fin,"%d %d",&n,&G);
    for(int i=0;i<n;i++)
    {
        fscanf(fin,"%d %d",&v[i].greutate,&v[i].profit);
    }
    fclose(fin);
    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];
    }
    
    fprintf(fout,"%d\n",Pmax);
    fclose(fout);
    return 0;
}