Cod sursa(job #3331686)

Utilizator ShokapKaplonyi Akos Shokap Data 29 decembrie 2025 22:34:56
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <vector>

std::ifstream input ("ruscsac.in");
std::ofstream output ("ruscsac.out");

struct object {
    int weight;
    int profit;
};

int main () {

    int n, maxWeight;
    input >> n >> maxWeight;

    std::vector<object> objectArr(n);
    for (int i = 0; i < n; i++){
        input >> objectArr[i].weight >> objectArr[i].weight;
    }

    //dpArr[i][w] = max profit at the i-th object with w max weight
    std::vector<std::vector<int>> dp(n, std::vector<int>(maxWeight+1));
    for (int w = 0; w <= maxWeight; w++){
        dp[0][w] = 0;
    }

    for (int i = 1; i < n; i++){
        for (int w = 0; w <= maxWeight; w++){
            dp[i][w] = std::max(dp[i-1][w], (objectArr[i].profit + dp[i-1][w - objectArr[i].weight]));
        }
    }

    output << dp[n][maxWeight];

    return 0;
}