Language: C++

#include <bits/stdc++.h>

using namespace std;


typedef long long ll;
typedef pair<int,ll> pl;

int N, Q, V[(1 << 18)], W[(1 << 18)], x[100005], L[100005], ans[100005], hasquery[(1 << 18)];
vector<pl> queries[(1 << 18)];
vector<pl> depth[(1 << 18)];

void dfs(int x, int d){
	//cerr << x << ' ' << d << '\n';
	vector<pl> v;
	//cerr << x << ' ' << x/2 << '\n';
	if (d == 10 || d == 1){
		depth[x].push_back(pl(0,0));
		depth[x].push_back(pl(W[x],V[x]));
	}
	else{
		for (auto it : depth[x/2]){
			if (it.first + W[x] > 100000) break;
			v.push_back(pl(it.first+W[x],it.second+V[x]));
		}
		int ptd = 0, ptv = 0;
		ll bval = -1;
		while (ptd < depth[x/2].size() && ptv < v.size()){
			if (depth[x/2][ptd].first < v[ptv].first){
				if (depth[x/2][ptd].second > bval){
					depth[x].push_back(depth[x/2][ptd]);
					bval = depth[x/2][ptd].second;
				}
				ptd++;
			}
			else{
				if (v[ptv].second > bval){
					depth[x].push_back(v[ptv]);
					bval = v[ptv].second;
				}
				ptv++;
			}
		}
		while (ptd < depth[x/2].size()){
			if (depth[x/2][ptd].second > bval){
				depth[x].push_back(depth[x/2][ptd]);
				bval = depth[x/2][ptd].second;
			}
			ptd++;
		}
		while (ptv < v.size()){
			if (v[ptv].second > bval){
				depth[x].push_back(v[ptv]);
				bval = v[ptv].second;
			}
			ptv++;
		}
	}
	
	
	/*
	cerr << x << '\n';
	for (auto it : depth[x]){
		cerr << it.first << ' ';
	}
	cerr << '\n';
	*/
	ll p = x;	
	if (d >= 10) p >>= (d-10+1);
	else p = 0;
	
	for (auto it : queries[x]){
		int pt1 = 0, pt2 = depth[p].size()-1;
		while (pt1 < depth[x].size()){
			while (pt2 >= 0 && depth[p][pt2].first + depth[x][pt1].first > it.first){
				pt2--;
			}
			if (pt2 >= 0) ans[it.second] = max(ans[it.second],depth[x][pt1].second+depth[p][pt2].second);
			pt1++;
		}
	}
	if (2*x <= N) dfs(2*x,d+1);
	if (2*x+1 <= N) dfs(2*x+1,d+1); 
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> N;
	for (int i = 1; i <= N; ++i){
		cin >> V[i] >> W[i];
	}
	cin >> Q;
	for (int i = 1; i <= Q; ++i){
		cin >> x[i] >> L[i];
		queries[x[i]].push_back(pl(L[i],i));
		while (x[i] > 0 && !hasquery[x[i]]){
			hasquery[x[i]] = 1;
			x[i] /= 2;
		}
	}
	for (int i = 1; i <= N; ++i){
		sort(queries[i].begin(),queries[i].end());
	}
	depth[0].push_back(pl(0,0));
	dfs(1,1);
	for (int i = 1; i <= Q; ++i){
		cout << ans[i] << '\n';
	}
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:78:59: error: no matching function for call to 'max(int&, long long int)'
   78 |                         if (pt2 >= 0) ans[it.second] = max(ans[it.second],depth[x][pt1].second+depth[p][pt2].second);
      |                                                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:78:59: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   78 |                         if (pt2 >= 0) ans[it.second] = max(ans[it.second],depth[x][pt1].second+depth[p][pt2].second);
      |                                                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:78:59: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   78 |                         if (pt2 >= 0) ans[it.second] = max(ans[it.second],depth[x][pt1].second+depth[p][pt2].second);
      |                                                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~