Language: C++

#include <bits/stdc++.h>
using namespace std;

// Inserted snippets: io, opt, root
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
// End snippets

struct It { int v, w; };

vector<pii> solve(vector<It> its, int L) {
	vector<pii> dp;
	dp.reserve(1 << its.size());

	dp.push_back({0, 0});

	for (It it : its) {
		vector<pii> dp2;
		dp2.reserve(sz(dp));
		for (auto p : dp) {
			if (p.first + it.w <= L) {
				dp2.push_back({ p.first + it.w, p.second + it.v });
			}
		}
		for (auto p : dp2) dp.push_back(p);
	}

	sort(all(dp), [](const pii a, const pii b) {
			if (a.first != b.first) return a.first < b.first;
			return a.second > b.second;
			});
	vector<pii> rv;
	rv.reserve(sz(dp))
	int cv = 0;

	for (auto p : dp) {
		chmax(cv, p.second);
		if (rv.empty() || p.first != rv.back().first)
			rv.push_back({ p.first, cv });
	}

	return rv;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

	int n; read(n);
	vector<It> kk(n + 1);
	rep(i, 1, n + 1) read(kk[i].v, kk[i].w);

	int q; read(q);
	rep(i, 0, q) {
		int v, l; read(v, l);
		
		vector<It> k;
		k.reserve(20);
		for (int j = v; j >= 1; j /= 2)
			k.push_back(kk[j]);
		vector<pii> p1 = solve(vector<It>(k.begin(), k.begin() + sz(k)/2), l);
		vector<pii> p2 = solve(vector<It>(k.begin() + sz(k)/2, k.end()), l);

/*	cout << "1" << endl;
	for (auto p : p1) write(p.first, p.second);
	cout << "2" << endl;
	for (auto p : p2) write(p.first, p.second);
*/
		int ans = 0;

		auto it = prev(p2.end());
		for (auto p : p1) {
			while (it != p2.begin() && it->first + p.first > l)
				--it;
			if (it == p2.end())
				continue;
			if (it->first + p.first > l)
				continue;
			chmax(ans, p.second + it->second);
		}

		write(ans);
	}
}


Compiler output:
a.cc: In function 'std::vector<std::pair<int, int> > solve(std::vector<It>, int)':
a.cc:46:27: error: expected ';' before 'int'
   46 |         rv.reserve(sz(dp))
      |                           ^
      |                           ;
   47 |         int cv = 0;
      |         ~~~                
a.cc:50:23: error: 'cv' was not declared in this scope; did you mean 'rv'?
   50 |                 chmax(cv, p.second);
      |                       ^~
      |                       rv
a.cc:52:37: error: no matching function for call to 'std::vector<std::pair<int, int> >::push_back(<brace-enclosed initializer list>)'
   52 |                         rv.push_back({ p.first, cv });
      |                         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
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,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; value_type = std::pair<int, int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::vector<std::pair<int, int> >::value_type&' {aka 'const std::pair<int, int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; value_type = std::pair<int, int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<std::pair<int, int> >::value_type&&' {aka 'std::pair<int, int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~