Cod sursa(job #3344207)

Utilizator Alex283810Mocan Alexandru Vali Alex283810 Data 1 martie 2026 17:24:09
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
int weight[5001];
int profit[5001];
std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");

int dp[5001];
int main()
{
    int n, G;
    fin >> n >> G;
    for(int i = 1; i <= n; i++)
    {
        fin >> weight[i] >> profit[i];
    }

    for(int i = 1; i <= n; i++)
    {
        for(int g = G; g >= 1; g--)
        {
            if(g >= weight[i])
            {
                dp[g] = std::max(dp[g], dp[g - weight[i]] + profit[i]);
            }
        }
    }
    fout << dp[G];
}