Language: C++

// #include "knapsack_1.h"
#include <iostream>
// #include <iterator>

#include <vector>

struct Item {
    int weight;
    int value;
};

using Memo = std::vector<std::vector<long long>>;

long long helper(const std::vector<Item>& items,
                 int capacity,
                 size_t pos,
                 long long value,
                 Memo& memo)
{
    if (capacity < 0)
        return 0;
    if (pos >= items.size())
        return value;

    auto& cached = memo[pos][capacity];
    if (cached != -1) {
        return cached;
    }

    long long skipping{helper(items, capacity, pos + 1, value, memo)};
    if (items[pos].weight > capacity)
        return skipping;
    long long taking{helper(items,
                            capacity - items[pos].weight,
                            pos + 1,
                            value + items[pos].value,
                            memo)};
    cached = std::max(skipping, taking);
    return cached;
}

long long max_value(const std::vector<Item>& items, int capacity)
{
    Memo memo(items.size(), std::vector<long long>(capacity + 1, -1));
    auto ans = helper(items, capacity, 0, 0, memo);
    return ans;
}

int main(int argc, char *argv[])
{
    int n, capacity;
    std::cin >> n >> capacity;
    std::vector<Item> items;
    items.reserve(n);

    for (int i = 0; i < n; ++i) {
        int weight, value;
        std::cin >> weight, value;
        items.emplace_back(weight, value);
    }
    std::cout << max_value(items, capacity) << std::endl;
    return 0;
}


Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Item; _Args = {int&, int&}; _Tp = Item]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = Item; _Args = {int&, int&}; _Tp = Item; allocator_type = std::allocator<Item>]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int&}; _Tp = Item; _Alloc = std::allocator<Item>; reference = Item&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:59:27:   required from here
   59 |         items.emplace_back(weight, value);
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: new initializer expression list treated as compound expression [-fpermissive]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'Item::Item(int&)'
a.cc:7:8: note: candidate: 'Item::Item()'
    7 | struct Item {
      |        ^~~~
a.cc:7:8: note:   candidate expects 0 arguments, 1 provided
a.cc:7:8: note: candidate: 'constexpr Item::Item(const Item&)'
a.cc:7:8: note:   no known conversion for argument 1 from 'int' to 'const Item&'
a.cc:7:8: note: candidate: 'constexpr Item::Item(Item&&)'
a.cc:7:8: note:   no known conversion for argument 1 from 'int' to 'Item&&'