Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

//vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
    #define cerr if (false) cerr
#endif

template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a)  {for (auto& i : a) out << i << " "; return out;}
template <typename T, typename U> void chkmin(T& a, U b) {if (a > b) a = b;}
template <typename T, typename U> void chkmax(T& a, U b) {if (a < b) a = b;}

#define v fs
#define w sc

vector <pii> items;
int lim, L = 1e9;

void rec(int i, vector <pii>& half, int val, int weight) {
    if (weight > L)
        return;
    if (i == lim) {
        half.eb(weight, val);
        return;
    }
    rec(i + 1, half, val, weight);
    rec(i + 1, half, val + items[i].v, weight + items[i].w);
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    vector <pii> a(n + 1);
    forn (i, 1, n + 1)
        cin >> a[i].v >> a[i].w;
    vector <int> levels(n + 1);
    forn (i, 1, n + 1)
        levels[i] = levels[i / 2] + 1; 

    const int TEN = 10;
    vector <vector <pii>> bottom(n + 1);
    forn (i, 1, n + 1) {
        if (levels[i] != TEN)
            continue;
        items.clear();
        int cur = i;
        while (cur) {
            items.pb(a[cur]);
            cur /= 2;
        }
        int m = sz(items);
        vector <pii> first_half;
        lim = sz(items);
        rec(0, first_half, 0, 0);
        stable_sort(all(first_half));
        bottom[i] = first_half;
    }
    int q;
    cin >> q;
    while (q --> 0) {
        int v;
        cin >> v >> L;
        if (levels[v] <= TEN) {
            items.clear();
            int cur = v;
            while (cur) {
                items.pb(a[cur]);
                cur /= 2;
            }
            int m = sz(items);
            vector <pii> first_half;
            lim = (m + 1) / 2;
            rec(0, first_half, 0, 0);

            reverse(all(items));
            vector <pii> second_half;
            lim = m / 2;
            rec(0, second_half, 0, 0);

            stable_sort(all(first_half));
            vector <int> pr(sz(first_half) + 1);
            forn (i, 0, sz(first_half))
                pr[i + 1] = max(pr[i], first_half[i].sc);
            stable_sort(all(second_half));
            int ans = 0, j = sz(first_half) - 1;
            forn (i, 0, sz(second_half)) {
                while (j >= 0 && first_half[j].fs + second_half[i].fs > L)
                    --j;
                /*int needed = L - second_half[i].fs;
                int j = lower_bound(all(weights), needed + 1) - weights.begin();*/
                chkmax(ans, second_half[i].sc + pr[j + 1]);
            }
            /*orn (i, 0, sz(items))
                cerr << items[i].fs << " " << items[i].sc << "\n";
            */
            cout << ans << "\n";
        } else {
            items.clear();
            int cur = v;
            while (levels[cur] > TEN) {
                items.pb(a[cur]);
                cur /= 2;
            }
            int m = sz(items);
            vector <pii> second_half;
            lim = m;
            rec(0, second_half, 0, 0);

            stable_sort(all(second_half));
            vector <int> pr(sz(bottom[cur]) + 1);
            forn (i, 0, sz(bottom[cur]))
                pr[i + 1] = max(pr[i], bottom[cur][i].sc);
            stable_sort(all(second_half));
            int ans = 0, j = sz(bottom[cur]) - 1;
            forn (i, 0, sz(second_half)) {
                while (j >= 0 && bottom[cur][j].fs + second_half[i].fs > L)
                    --j;
                /*int needed = L - second_half[i].fs;
                int j = lower_bound(all(weights), needed + 1) - weights.begin();*/
                chkmax(ans, second_half[i].sc + pr[j + 1]);
            }
            /*orn (i, 0, sz(items))
                cerr << items[i].fs << " " << items[i].sc << "\n";
            */
            cout << ans << "\n";
        }
    }
    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:4:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int> > >::_Vector_impl::~_Vector_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::pair<int, int>]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/stl_vector.h:132:14: note: called from here
  132 |       struct _Vector_impl
      |              ^~~~~~~~~~~~