Language: C++

#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 1<<19;
int n, q, v[maxn], w[maxn];
int query(int x, int lim) {
	//cerr << "Q " << x << '\n';
	vector<pair<int,int>> a;
	a.reserve(18);
	for (;x;x>>=1) 
		a.pb(v[x], w[x]);
	if (a.size() == 1) return w[x] > lim ? 0 : v[x];
	int o = a.size()/2, p = a.size()-o;
	int res = 0;
//	cerr << "A " << '\n';
//	for (auto [v, w] : a) cerr << "W V " << w << ' ' << v << '\n';
//	cerr << '\n';
	vector<pair<int,int>> b, c;
	b.reserve(512), c.reserve(512);
	//cerr << "PB " << '\n';
	for (int i = 0;i < 1<<o;++i) {
		int V = 0, W = 0;
		for (int j = 0;j < o;++j)
			if (i&(1<<j)) V += a[j].first, W += a[j].second;
		//cerr << "W V " << W << ' ' << V << '\n';
		if (W <= lim) b.pb(W, V);
	}
	//cerr << "PC \n";

	for (int i = 0;i < 1<<p;++i) {
		int V = 0, W = 0;
		for (int j = 0;j < p;++j)
			if (i&(1<<j)) V += a[j+o].first, W += a[j+o].second;//, cerr << "I+O " << i+o << '\n';
		//cerr << "W V " << W << ' ' << V << '\n';
		if (W <= lim) c.pb(W, V);
	}
	sort(b.begin(), b.end()), sort(c.begin(), c.end());
//	cerr << "B "<< '\n';
//	for (auto [w, v] : b) cerr << "W V " << w << ' ' << v << '\n';
//	cerr << "C " << '\n';
//	for (auto [w, v] : c) cerr << "W V " << w << ' ' << v << '\n';
//	cerr << '\n';
	for (int i = 1;i < b.size();++i) 
		b[i].second = max(b[i].second, b[i-1].second);
	for (int i = 1;i < c.size();++i)
		c[i].second = max(c[i].second, c[i-1].second);

	res = max(b[i].back().second, c[i].back().second);

	for (int i = 0, j = (int)c.size()-1;i < b.size();++i) {
		while (j && c[j].first + b[i].first > lim) --j;
		res = max(res, c[j].second + b[i].second);
	}
	return res;
}


inline char readchar(){static const size_t bufsize = 1 << 16; static size_t p = 0, end = 0; static char buf[bufsize]; if (p == end) end = fread_unlocked(buf, sizeof(char), bufsize, stdin), p = 0; return p == end ? EOF : buf[p++]; }
inline void read(char &a){while(isspace(a = readchar()));}
inline void read(char *a){while(isspace(*a = readchar()));++a; while(isgraph(*a = readchar()))++a;}
inline void read(string &a){static char p; while(isspace(p = readchar())); a = p; while(isgraph(p = readchar()))a.push_back(p);}
template<typename T> inline void read(T& a){static char p; static bool b;while (!isdigit(p = readchar()))b = p == '-'; a = p ^ '0'; while (isdigit(p = readchar())) a *= 10, a += p ^ '0'; if(b)a *= -1, b = false; }
template<typename T1, typename T2> inline void read(pair<T1, T2> &a){read(a.first), read(a.second);}
template<typename T> inline void read(T begin, const T &end){while(begin != end)read(*begin), ++begin;}
inline void R(){} template<class T1, class... T2> inline void R(T1 &h, T2 &... e){read(h), R(e...);}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	R(n);
	//cin >> n;
	for (int i = 1;i <= n;++i)
		R(v[i], w[i]);
		//cin >> v[i] >> w[i];
	R(q);
	//cin >> q;
	while (q--) {
		int v, lim;
		R(v, lim);
		//cin >> v >> lim;
		cout << query(v, lim) << '\n';
	}
}



Compiler output:
a.cc: In function 'int query(int, int)':
a.cc:49:21: error: 'i' was not declared in this scope
   49 |         res = max(b[i].back().second, c[i].back().second);
      |                     ^