Repository: TheAlgorithms/C-Plus-Plus
File: dynamic_programming/Unbounded_0_1_Knapsack.cpp (C++)

Code before:
 * @brief Namespace for dynamic programming algorithms
 */
namespace dynamic_programming {

/**
 * @namespace Knapsack
 * @brief Implementation of unbounded 0-1 knapsack problem
 */
namespace unbounded_knapsack {

/**
 * @brief Recursive function to calculate the maximum value obtainable using 
 *        an unbounded knapsack approach.
 *
 * @param i Current index in the value and weight vectors.
 * @param W Remaining capacity of the knapsack.
 * @param val Vector of values corresponding to the items.
 * @param wt Vector of weights corresponding to the items.
 * @param dp 2D vector for memoization to avoid redundant calculations.
 * @return The maximum value that can be obtained for the given index and capacity.
 */
int KnapSackFilling(std::uint16_t i, std::uint16_t W, 
                    const std::vector<std::uint16_t>& val, 
                    const std::vector<std::uint16_t>& wt, 
                    std::vector<std::vector<int>>& dp) {
    if (i == 0) {
        if (wt[0] <= W) {
            return (W / wt[0]) * val[0]; // Take as many of the first item as possible
        } else {
            return 0; // Can't take the first item
        }
    }
    if (dp[i][W] != -1) return dp[i][W]; // Return result if available

    int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
    int take = 0;
    if (W >= wt[i]) {
        take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
    }
    return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
}

/**
 * @brief Wrapper function to initiate the unbounded knapsack calculation.
 *
 * @param N Number of items.
 * @param W Maximum weight capacity of the knapsack.
 * @param val Vector of values corresponding to the items.
 * @param wt Vector of weights corresponding to the items.
 * @return The maximum value that can be obtained for the given capacity.
 */

Review comment (suggestion):
Couldn't values and weights be a std::pair?

That is
```cpp
std::vector<std::pair<std::uint16_t, std::uint16_t> data
```

Code after:
 * @brief Namespace for dynamic programming algorithms
 */
namespace dynamic_programming {

/**
 * @namespace Knapsack
 * @brief Implementation of unbounded 0-1 knapsack problem
 */
namespace unbounded_knapsack {

/**
 * @brief Recursive function to calculate the maximum value obtainable using 
 *        an unbounded knapsack approach.
 *
 * @param i Current index in the value and weight vectors.
 * @param W Remaining capacity of the knapsack.
 * @param val Vector of values corresponding to the items.
 * @note "val" data type can be changed according to the size of the input.
 * @param wt Vector of weights corresponding to the items.
 * @note "wt" data type can be changed according to the size of the input.
 * @param dp 2D vector for memoization to avoid redundant calculations.
 * @return The maximum value that can be obtained for the given index and capacity.
 */
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, 
                    const std::vector<std::uint16_t>& val, 
                    const std::vector<std::uint16_t>& wt, 
                    std::vector<std::vector<int>>& dp) {
    if (i == 0) {
        if (wt[0] <= W) {
            return (W / wt[0]) * val[0]; // Take as many of the first item as possible
        } else {
            return 0; // Can't take the first item
        }
    }
    if (dp[i][W] != -1) return dp[i][W]; // Return result if available

    int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
    int take = 0;
    if (W >= wt[i]) {
        take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
    }
    return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
}

/**
 * @brief Wrapper function to initiate the unbounded knapsack calculation.
 *
 * @param N Number of items.
 * @param W Maximum weight capacity of the knapsack.
 * @param val Vector of values corresponding to the items.
 * @param wt Vector of weights corresponding to the items.