Language: C++

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_map>

using namespace std;

typedef long long ll;

const int sz = (1 << 18) + 20;
const int sz2 = (1 << 10) + 50;

int n, q;
int dp[sz2][sz], mx[sz2][sz];
int vw[sz][2];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> vw[i][1] >> vw[i][0];
	}

	for (int i = 1; i <= sz2; i++)
		for (int j = 0; j < sz; j++)
			dp[i][j] = -1;

	dp[1][0] = 0;
	dp[1][vw[1][0]] = vw[1][1];

	mx[1][0] = dp[1][0];
	for (int j = 1; j <= sz - 1; j++)
		mx[1][j] = max(mx[1][j - 1], dp[1][j]);

	for (int i = 2; i <= min(n, sz2); i++) {
		for (int j = sz - 1; j >= 0; j--) {
			if (dp[i / 2][j] == -1)
				continue;
			dp[i][j] = max(dp[i][j], dp[i / 2][j]);
			if (j + vw[i][0] < sz)
				dp[i][j + vw[i][0]] = max(dp[i][j + vw[i][0]], dp[i / 2][j] + vw[i][1]);
		}
		mx[i][0] = dp[i][0];
		for (int j = 1; j <= sz - 1; j++)
			mx[i][j] = max(mx[i][j - 1], dp[i][j]);
	}

	cin >> q;
	for (int i = 0; i < q; i++) {
		int v, l;
		cin >> v >> l;
		if (v <= sz2) {
			cout << mx[v][l];
		}
		else {
			vector<int> anc;
			int u = v;
			while (u != 0) {
				anc.push_back(u);
				u >>= 1;
			}
			int ind = anc.size();
			for (int j = 0; j < (int)anc.size(); j++)
				if (anc[j] <= sz2) {
					ind = j;
					break;
				}
			int otv = 0;
			for (int j = 0; j < (1 << ind); j++) {
				int w = 0, val = 0;
				for (int k = 0; k < ind; k++)
					if (((j >> k) & 1) == 1) {
						w += vw[anc[k]][0];
						val += vw[anc[k]][1];
					}
				if (w > l)
					continue;
				otv = max(otv, val + mx[anc[ind]][l - w]);
			}

			cout << otv;
		}
		cout << '\n';
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int q = 1;
	//cin >> q;
	for (; q > 0; q--) {
		solve();
		cout << '\n';
	}
}

Compiler output:
/tmp/cciRnW0B.o: in function `solve()':
a.cc:(.text+0x3b): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x67): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0xeb): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0xf1): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x28e): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x2e7): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x303): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x353): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x61b): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
a.cc:(.text+0x649): relocation truncated to fit: R_X86_64_PC32 against symbol `vw' defined in .bss section in /tmp/cciRnW0B.o
collect2: error: ld returned 1 exit status