Cod sursa(job #2981421)

Utilizator TheAndreiEnache Andrei Alexandru TheAndrei Data 17 februarie 2023 21:36:18
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>

#define NMAX 6
#define GMAX 11

using namespace std;

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

struct obiect{
    int greutate, profit;
};

obiect v[NMAX+1];
int best[2][GMAX];

int main()
{
    int n, g;
    fin>>n>>g;

    for(int i=1;i<=n;i++){
        fin>>v[i].greutate>>v[i].profit;
    }

    for(int i=1;i<=n;i++)
        for(int j=1;j<=g;j++){
            best[i&1][j]=best[(i-1)&1][j];
            if(j>=v[i].greutate)
                best[i&1][j]=std::max(best[(i-1)&1][j], best[(i-1)&1][j-v[i].greutate]+v[i].profit);
        }

    fout<<best[n&1][g];

    return 0;
}