Cod sursa(job #3131382)

Utilizator AndreiLeusteanLeustean Andrei AndreiLeustean Data 19 mai 2023 22:45:38
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

struct obiect
{
    int g, v;
};
int main()
{
    int n, G;
    in>>n>>G;
    obiect ob[n+1];
    for(int i=1; i<=n; i++)
        in>>ob[i].g>>ob[i].v;
    int mat[n+1][G+1];
    for(int i=0; i<=n; i++)
        mat[i][0]=0;
    for(int j=0; j<=G; j++)
        mat[0][j]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=G;j++)
        {
            if(ob[i].g>j)
                mat[i][j]=0;
            else
            {
                mat[i][j]=max(mat[i-1][j], mat[i-1][j-ob[i].g]+ob[i].v);
            }
        }

    }
    out<<mat[n][G];
    return 0;
}