Cod sursa(job #2850352)

Utilizator oporanu.alexAlex Oporanu oporanu.alex Data 16 februarie 2022 17:33:23
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmax = 55;
const int wmax = 105;

int dp[nmax][wmax];

struct object {
    int weight;
    int value;
};

int main() {

    int N, maxWeight;
    fin >> N >> maxWeight;
    vector<object> objects(N + 1);

    for(int i = 1; i <= N; ++i) {
        fin >> objects[i].weight;
        fin >> objects[i].value;
    }

    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= maxWeight; ++j)
            if(j >= objects[i].weight)
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - objects[i].weight] + objects[i].value);
            else dp[i][j] = dp[i - 1][j];

    fout << dp[N][maxWeight] << '\n';



    return 0;
}