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> dp,dp2,rv;

Vector<pii> solve(Vector<It>::iterator it1, Vector<It>::iterator it2, int L) {
	dp.clear();
	dp.push_back({0, 0});

	for (auto ii=it1; ii!=it2; ii++) {
		int s=sz(dp);
		auto it=*ii;
		for (int i=0;i<s;i++) {
			auto p=dp[i];
			if (p.first + it.w <= L) {
				dp.push_back({ p.first + it.w, p.second + it.v });
			}
		}
	}

	sort(all(dp), [](const pii a, const pii b) {
			if (a.first != b.first) return a.first < b.first;
			return a.second > b.second;
			});
	rv.clear();
	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;
}

Vector<It> k;

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);

	dp.reserve(1<<10);
	dp2.reserve(1<<10);
	rv.reserve(1<<10);
	k.reserve(20);

	int q; read(q);
	rep(i, 0, q) {
		int v, l; read(v, l);

		k.clear();
		for (int j = v; j >= 1; j /= 2)
			k.push_back(kk[j]);
		Vector<pii> p1 = solve(k.begin(), k.begin()+sz(k)/2, l);
		Vector<pii> p2 = solve(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->first + p.first > l)
				continue;
			chmax(ans, p.second + it->second);
		}

		write(ans);
	}
}


Compiler output:
a.cc:24:1: error: 'Vector' does not name a type
   24 | Vector<pii> dp,dp2,rv;
      | ^~~~~~
a.cc:26:1: error: 'Vector' does not name a type
   26 | Vector<pii> solve(Vector<It>::iterator it1, Vector<It>::iterator it2, int L) {
      | ^~~~~~
a.cc:57:1: error: 'Vector' does not name a type
   57 | Vector<It> k;
      | ^~~~~~
a.cc: In function 'int main()':
a.cc:64:9: error: 'Vector' was not declared in this scope
   64 |         Vector<It> kk(n + 1);
      |         ^~~~~~
a.cc:64:18: error: expected primary-expression before '>' token
   64 |         Vector<It> kk(n + 1);
      |                  ^
a.cc:64:20: error: 'kk' was not declared in this scope
   64 |         Vector<It> kk(n + 1);
      |                    ^~
a.cc:67:9: error: 'dp' was not declared in this scope; did you mean 'dup'?
   67 |         dp.reserve(1<<10);
      |         ^~
      |         dup
a.cc:68:9: error: 'dp2' was not declared in this scope; did you mean 'dup2'?
   68 |         dp2.reserve(1<<10);
      |         ^~~
      |         dup2
a.cc:69:9: error: 'rv' was not declared in this scope
   69 |         rv.reserve(1<<10);
      |         ^~
a.cc:70:9: error: 'k' was not declared in this scope
   70 |         k.reserve(20);
      |         ^
a.cc:79:27: error: expected primary-expression before '>' token
   79 |                 Vector<pii> p1 = solve(k.begin(), k.begin()+sz(k)/2, l);
      |                           ^
a.cc:79:29: error: 'p1' was not declared in this scope; did you mean 'y1'?
   79 |                 Vector<pii> p1 = solve(k.begin(), k.begin()+sz(k)/2, l);
      |                             ^~
      |                             y1
a.cc:79:34: error: 'solve' was not declared in this scope
   79 |                 Vector<pii> p1 = solve(k.begin(), k.begin()+sz(k)/2, l);
      |                                  ^~~~~
a.cc:80:27: error: expected primary-expression before '>' token
   80 |                 Vector<pii> p2 = solve(k.begin()+sz(k)/2, k.end(), l);
      |                           ^
a.cc:80:29: error: 'p2' was not declared in this scope
   80 |                 Vector<pii> p2 = solve(k.begin()+sz(k)/2, k.end(), l);
      |                             ^~