Language: C++

#pragma GCC optimize("O2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline-functions")
#pragma GCC target("sse4,avx2,abm,fma,tune=native")

#include <cstdio>
#include <algorithm>

typedef std::vector<std::pair<int, int>> vp;

const int N = 1 << 18;

int n, q;
int v[N], w[N];

void dfs(const vp &a, vp &s, int k, int sw, int sv) {
	if (k == (int)a.size()) {
		s.push_back({sw, sv});
		return;
	}
	dfs(a, s, k + 1, sw, sv);
	dfs(a, s, k + 1, sw + a[k].first, sv + a[k].second);
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", v + i, w + i);
	}
	scanf("%d", &q);
	while (q--) {
		int u, m;
		scanf("%d%d", &u, &m);
		vp a;
		for (int i = u; i; i /= 2) {
			a.push_back({w[i], v[i]});
		}
		int B = std::min((int)a.size() / 2, 8);
		vp s1, s2;
		dfs(vp(a.begin(), a.begin() + B), s1, 0, 0, 0);
		dfs(vp(a.begin() + B, a.end()), s2, 0, 0, 0);
		std::sort(s1.begin(), s1.end());
		for (int i = 1; i < (int)s1.size(); ++i) {
			s1[i].second = std::max(s1[i].second, s1[i - 1].second);
		}
		int ans = 0;
		for (int i = 0; i < (int)s2.size(); ++i) {
			std::pair<int, int> tmp = {m - s2[i].first, 1e9};
			auto it = std::upper_bound(s1.begin(), s1.end(), tmp);
			int j = it - s1.begin() - 1;
			if (j >= 0) {
				ans = std::max(ans, s2[i].second + s1[j].second);
			}
		}
		printf("%d\n", ans);
	}
}

Compiler output:
a.cc:11:14: error: 'vector' in namespace 'std' does not name a template type
   11 | typedef std::vector<std::pair<int, int>> vp;
      |              ^~~~~~
a.cc:10:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    9 | #include <algorithm>
  +++ |+#include <vector>
   10 | 
a.cc:18:16: error: 'vp' does not name a type
   18 | void dfs(const vp &a, vp &s, int k, int sw, int sv) {
      |                ^~
a.cc:18:23: error: 'vp' has not been declared
   18 | void dfs(const vp &a, vp &s, int k, int sw, int sv) {
      |                       ^~
a.cc: In function 'void dfs(const int&, int&, int, int, int)':
a.cc:19:25: error: request for member 'size' in 'a', which is of non-class type 'const int'
   19 |         if (k == (int)a.size()) {
      |                         ^~~~
a.cc:20:19: error: request for member 'push_back' in 's', which is of non-class type 'int'
   20 |                 s.push_back({sw, sv});
      |                   ^~~~~~~~~
a.cc:24:32: error: invalid types 'const int[int]' for array subscript
   24 |         dfs(a, s, k + 1, sw + a[k].first, sv + a[k].second);
      |                                ^
a.cc:24:49: error: invalid types 'const int[int]' for array subscript
   24 |         dfs(a, s, k + 1, sw + a[k].first, sv + a[k].second);
      |                                                 ^
a.cc: In function 'int main()':
a.cc:36:17: error: 'vp' was not declared in this scope; did you mean 'v'?
   36 |                 vp a;
      |                 ^~
      |                 v
a.cc:38:25: error: 'a' was not declared in this scope
   38 |                         a.push_back({w[i], v[i]});
      |                         ^
a.cc:40:39: error: 'a' was not declared in this scope
   40 |                 int B = std::min((int)a.size() / 2, 8);
      |                                       ^
a.cc:41:19: error: expected ';' before 's1'
   41 |                 vp s1, s2;
      |                   ^~~
      |                   ;
a.cc:42:51: error: 's1' was not declared in this scope
   42 |                 dfs(vp(a.begin(), a.begin() + B), s1, 0, 0, 0);
      |                                                   ^~
a.cc:43:49: error: 's2' was not declared in this scope
   43 |                 dfs(vp(a.begin() + B, a.end()), s2, 0, 0, 0);
      |                                                 ^~
a.cc:50:72: error: could not convert '{<expression error>, 1.0e+9}' from '<brace-enclosed initializer list>' to 'std::pair<int, int>'
   50 |                         std::pair<int, int> tmp = {m - s2[i].first, 1e9};
      |                                                                        ^
      |                                                                        |
      |                                                                        <brace-enclosed initializer list>