Language: C++

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

const int M = 512;

int main()
{
    int n; scanf("%d", &n);

    vector<int> v(n + 1), w(n + 1);
    vector<map<int, int>> m(M);

    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &v[i], &w[i]);
        if (i >= M) continue;

        m[i].emplace(w[i], v[i]);

        for (int j = i / 2; j > 0; j /= 2) {
            for (const auto& wv : m[j]) {
                const int wj = wv.first;
                const int vj = wv.second;
                m[i].emplace(wi + wj, vi + vj);
                m[i].emplace(wj, vj);
            }
        }
        for (auto& wv : m[i]) {
            prev = wv.second = max(prev, wv.second);
        }
    }

    return 0;

    int q; scanf("%d", &q);

    for (int i = 0; i < q; i++) {
        int v, l; scanf("%d%d", &v, &l);
        int ans = 0;
        int u = v;
        while (u >= M)
            u /= 2;
        for (int i = u; i >= 1; i /= 2) {
            auto it = m[i].upper_bound(l);
            if (it != m[i].begin())
                ans = max(ans, (--it)->second);
        }
        printf("%d\n", ans);
    }

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:27:30: error: 'wi' was not declared in this scope; did you mean 'wj'?
   27 |                 m[i].emplace(wi + wj, vi + vj);
      |                              ^~
      |                              wj
a.cc:27:39: error: 'vi' was not declared in this scope; did you mean 'vj'?
   27 |                 m[i].emplace(wi + wj, vi + vj);
      |                                       ^~
      |                                       vj
a.cc:32:35: error: no matching function for call to 'max(<unresolved overloaded function type>, int&)'
   32 |             prev = wv.second = max(prev, wv.second);
      |                                ~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:20: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'const int&'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |         ~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:32:35: note:   couldn't deduce template parameter '_Tp'
   32 |             prev = wv.second = max(prev, wv.second);
      |                                ~~~^~~~~~~~~~~~~~~~~