Language: C++

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<ll> v(n), w(n);
    for(ll i=0; i<n; i++)
        cin >> v[i] >> w[i];

    ll q;
    cin >> q;
    map<ll,vector<tuple<ll,ll,ll>>> qs;
    set<ll> sss;
    for(ll i=0; i<q; i++) {
        ll x, l;
        cin >> x >> l;
        x--;
        qs[x].push_back({x, l, i});
        while(1) {
            sss.insert(x);
            if(!x) break;
            x = (x-1)/2;
        }
    }

    vector<ll> res(q);

    vector<vector<int>> u(1<<11, vector<int>(1e5+1));
    for(ll i=0; i<min((ll)1<<11, n); i++) {
        for(ll j=0; j<=1e5; j++)
            u[i][j] = max({u[i][j], (j-w[i] >= 0 ? u[(i-1)/2][j-w[i]] + v[i] : -1), u[(i-1)/2][j]});
        for(ll j=1; j<=1e5; j++)
            u[i][j] = max(u[i][j], u[i][j-1]);
    }

    auto dfs = [&](auto dfs, ll j, const map<ll,ll> &pre) -> void {
        if(!sss.count(j)) return;

        map<ll,ll> t = pre;

        if(j < 1<<11) {
            if(qs.count(j) > 0)
                for(auto [x, l, i]: qs[j])
                    res[i] = u[j][l];
        } else { 
            for(auto [ww, vv]: pre)
                if(ww+w[j] <= 1e5)
                    t[ww + w[j]] = max(t[ww + w[j]], vv + v[j]);

            if(qs.count(j) > 0) {
                for(auto [x, l, i]: qs[j]) {
                    while(x >= 1<<11) x = (x-1)/2;

                    ll r = 0;
                    for(auto &[ww,vv]: t) {
                        if(l - ww < 0) continue;

                        r = max(r, vv + u[x][l-ww]);
                    }
                    res[i] = r;
                }
            }
        }
        if(2*j+1 < n) dfs(dfs, 2*j+1, t);
        if(2*j+2 < n) dfs(dfs, 2*j+2, t);
    };
    map<ll,ll> t;
    t[0] = 0;
    dfs(dfs, 0, t);
    for(auto &e: res) cout << e << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:40:26: error: no matching function for call to 'max(<brace-enclosed initializer list>)'
   40 |             u[i][j] = max({u[i][j], (j-w[i] >= 0 ? u[(i-1)/2][j-w[i]] + v[i] : -1), u[(i-1)/2][j]});
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 1 provided
/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, 1 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:   template argument deduction/substitution failed:
a.cc:40:26: note:   deduced conflicting types for parameter '_Tp' ('int' and 'long long int')
   40 |             u[i][j] = max({u[i][j], (j-w[i] >= 0 ? u[(i-1)/2][j-w[i]] + v[i] : -1), u[(i-1)/2][j]});
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:   candidate expects 2 arguments, 1 provided