Cod sursa(job #2551797)

Utilizator RedPipperNastasa Stefan-Alexandru RedPipper Data 20 februarie 2020 10:51:15
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

struct object
{
    int weight, value;
};

int n,g;
object lst[5001];
int dp[5001][10001];

void citire()
{
    fin>>n>>g;
    for(int i=1;i<=n;++i)
    {
        fin>>lst[i].weight;
        fin>>lst[i].value;
    }

}

int main()
{
    citire();
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=g;++j)
        {
            if(j-lst[i].weight<0)
                dp[i][j] = dp[i-1][j];
            else
            {
                int bef = j-lst[i].weight;
                dp[i][j] = max(dp[i-1][bef] + lst[i].value, dp[i-1][j]);
            }
        }
    }
    fout<<dp[n][g];

    return 0;
}