Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define un(x)           x.erase(unique(all(x)), x.end())
#define xx              first
#define yy              second
#define pb              push_back
#define all(v)          v.begin(), v.end()

#define D(x)            cerr << #x " = " << x << '\n'
#define DBG             cerr << "Hi!" << '\n'

#define CLR(a)          memset(a, 0, sizeof(a))
#define SET(a)          memset(a, -1, sizeof(a))

// inline int setBit(int n, int pos) { return n = n | (1 << pos); }
// inline int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
// inline bool checkBit(int n, int pos) { return (bool)(n & (1 << pos)); }

// int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
// int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// unsigned int num = rng();
// shuffle(V.begin(), V.end(), rng);

/******************************************************************************************/

const int MAX = 300005;
const int INF = 0x3f3f3f3f;
// const int MOD = 1000000007;

// inline int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
// inline int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
// inline int mul(int a, int b) { return (a * 1LL * b) % MOD; }

set < pii > S;
void insert(pii p) {
    S.insert(p);
    auto it = S.find(p);
    if(it != S.begin()){
        auto tmp = it;
        --tmp;
        if(tmp->yy >= it->yy){
            S.erase(it);
            return;
        }
    }
    ++it;
    while(it != S.end() && it->yy <= p.yy){
        S.erase(it);
        it = S.find(p);
        ++it;
    }
}
inline int query(int v) {
    if(S.empty()) return 0;
    auto it = S.upper_bound({v, INF});
    if(it == S.begin()) return 0;
    return (--it)->second;
}


int V[MAX], W[MAX], L;
vector <pii> lft, rgt;
int ans;
int l, r;

void generate_left(int p, int sw, int sv) {
    if(sw > L) return;
    if(p >= l) {
        insert(make_pair(sw, sv));
        return;
    }
    generate_left(p + 1, sw, sv);
    generate_left(p + 1, sw + lft[p].xx, sv + lft[p].yy);
}

void generate_right(int p, int sw, int sv) {
    if(sw > L) return;
    if(p >= r) {
        ans = max(ans, query(L - sw) + sv);
        return;
    }
    generate_right(p + 1, sw, sv);
    generate_right(p + 1, sw + rgt[p].xx, sv + rgt[p].yy);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    // freopen("in", "r", stdin); 
    // freopen("out", "w", stdout);
    
    int n; cin >> n;
    for(int i=1;i<=n;i++) cin >> V[i] >> W[i];

    int q, v; cin >> q;
    for(int i=1;i<=q;i++) {
        cin >> v >> L;
        while(v) {
            if(l <= r) lft.emplace_back(W[v], V[v]), l++;
            else rgt.emplace_back(W[v], V[v]), r++;
            v >>= 1;
        }
        ans = 0;
        S.clear();
        generate_left(0, 0, 0);
        generate_right(0, 0, 0);
        cout << ans << '\n';
        lft.clear();
        rgt.clear();
        l = r = 0;
    }
    return 0;
}



Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:5:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, std::less<std::pair<int, int> >, std::allocator<std::pair<int, int> > >::_Rb_tree_impl<std::less<std::pair<int, int> >, true>::~_Rb_tree_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::_Rb_tree_node<std::pair<int, int> >]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152:
/usr/include/c++/14/bits/stl_tree.h:658:16: note: called from here
  658 |         struct _Rb_tree_impl
      |                ^~~~~~~~~~~~~