Language: C++

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

// Inserted snippets: io, 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); }
// 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;
		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));
	vector<pii> rv;
	int cv = 0;

	for (auto p : dp) {
		chmax(cv, p.second);
		rv.push_back({ p.first, cv });
	}

	return move(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;

		for (auto p : p1) {
			auto it = upper_bound(all(p2), { l - p.first, 0 });
			if (it == p2.begin())
				continue;
			--it;
			chmax(ans, p.second + it->second);
		}

		write(ans);
	}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:77:46: error: no matching function for call to 'upper_bound(std::vector<std::pair<int, int> >::iterator, std::vector<std::pair<int, int> >::iterator, <brace-enclosed initializer list>)'
   77 |                         auto it = upper_bound(all(p2), { l - p.first, 0 });
      |                                   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:2019:5: note: candidate: 'template<class _FIter, class _Tp> _FIter std::upper_bound(_FIter, _FIter, const _Tp&)'
 2019 |     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2019:5: note:   template argument deduction/substitution failed:
a.cc:77:46: note:   couldn't deduce template parameter '_Tp'
   77 |                         auto it = upper_bound(all(p2), { l - p.first, 0 });
      |                                   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2050:5: note: candidate: 'template<class _FIter, class _Tp, class _Compare> _FIter std::upper_bound(_FIter, _FIter, const _Tp&, _Compare)'
 2050 |     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2050:5: note:   candidate expects 4 arguments, 3 provided