Language: C++

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

#include <vector>

// struct Item {
//     int weight;
//     int value;
//
//     Item(int w, int v)
//         : weight{w}
//         , value{v}
//     {
//     }
// };

long long max_value(const std::vector<Item>& items, int capacity)
{
    std::vector<long long> dp(capacity + 1);
    std::vector<long long> prev = dp;

    for (const auto& item : items) {
        for (int w = 0; w <= capacity; ++w) {
            if (item.weight > w)
                dp[w] = prev[w];
            else {
                dp[w] = std::max(prev[w], item.value + prev[w - item.weight]);
            }
        }
        std::swap(dp, prev);
    }

    return prev.back();
}

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:
a.cc:18:39: error: 'Item' was not declared in this scope; did you mean 'tm'?
   18 | long long max_value(const std::vector<Item>& items, int capacity)
      |                                       ^~~~
      |                                       tm
a.cc:18:43: error: template argument 1 is invalid
   18 | long long max_value(const std::vector<Item>& items, int capacity)
      |                                           ^
a.cc:18:43: error: template argument 2 is invalid
a.cc: In function 'long long int max_value(const int&, int)':
a.cc:23:29: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   23 |     for (const auto& item : items) {
      |                             ^~~~~
      |                             std::begin
In file included from /usr/include/c++/14/string:53,
                 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/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:23:29: error: 'end' was not declared in this scope; did you mean 'std::end'?
   23 |     for (const auto& item : items) {
      |                             ^~~~~
      |                             std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc: In function 'int main(int, char**)':
a.cc:41:17: error: 'Item' was not declared in this scope; did you mean 'tm'?
   41 |     std::vector<Item> items;
      |                 ^~~~
      |                 tm
a.cc:41:21: error: template argument 1 is invalid
   41 |     std::vector<Item> items;
      |                     ^
a.cc:41:21: error: template argument 2 is invalid
a.cc:42:11: error: request for member 'reserve' in 'items', which is of non-class type 'int'
   42 |     items.reserve(n);
      |           ^~~~~~~
a.cc:47:15: error: request for member 'emplace_back' in 'items', which is of non-class type 'int'
   47 |         items.emplace_back(weight, value);
      |               ^~~~~~~~~~~~