Language: C++

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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
 
////////////////////////////////////////////////////////////////////

const int N = 1e5 + 5, K = 9, M = 1 << (K + 1) + 5;

int n, V[4 << K + 5], W[4 << K + 5], SumV[1 << K], SumW[1 << K], DP[M][N];
vector<int> P;

void DFS(int v) {
	if (v > n || v >= (1 << K)) return;	
	P.push_back(v);
	for (int mask = 1; mask < (1 << sz(P)); mask++) {
		SumV[mask] = SumV[mask - (mask & - mask)] + V[P[__builtin_ctz(mask)]];
		SumW[mask] = SumW[mask - (mask & - mask)] + W[P[__builtin_ctz(mask)]];
		if (SumW[mask] < N) DP[v][SumW[mask]] = max(DP[v][SumW[mask]], SumV[mask]);
	}
	for (int i = 1; i < N; i++) DP[v][i] = max(DP[v][i], DP[v][i - 1]);
	DFS(v << 1), DFS((v << 1) | 1);
	P.pop_back();
}

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));
	
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> V[i] >> W[i];
	DFS(1);
	int q; cin >> q;
	for (int i = 0; i < q; i++) {
		int v, l; cin >> v >> l;
		P.clear();
		while (v >= (1 << K)) P.push_back(v), v >>= 1;
		int ans = DP[v][l];
		for (int mask = 0; mask < sz(P); mask++) {
			SumV[mask] = SumV[mask - (mask & - mask)] + V[P[__builtin_ctz(mask)]];
			SumW[mask] = SumW[mask - (mask & - mask)] + W[P[__builtin_ctz(mask)]];
			if (SumW[mask] <= l) ans = max(ans, SumV[mask] + DP[v][l - SumW[mask]]);
		}
		cout << ans << endl;
	}

	return 0;
}


Compiler output:
/tmp/ccFbhkJe.o: in function `DFS(int)':
a.cc:(.text+0x50): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x94): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0xfc): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x1fe): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x2e4): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
/tmp/ccFbhkJe.o: in function `main':
a.cc:(.text+0x435): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x450): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x4e2): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x54a): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
a.cc:(.text+0x633): relocation truncated to fit: R_X86_64_PC32 against symbol `P' defined in .bss section in /tmp/ccFbhkJe.o
/tmp/ccFbhkJe.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x696): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status