Language: C++

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

int main() {
	ll n; cin >> n;
	vector<vector<ll> > p(n+1, vector<ll>(2));
	for (ll i = 0; i < n; ++i) {
		ll v, w; cin >> v >> w;
		p[i+1][0] = v; p[i+1][1] = w;
	}
	ll k = 1;
	while (n > (1<<k)-1) k++;
	k = min(k, 9);
	vector<vector<ll> > dp((1<<k), vector<ll>(100001, 0));
	for (ll i = p[1][1]; i < 100001; ++i) dp[1][i] = p[1][0];
	for (ll i = 2; i < (1<<k); ++i) {
		for (ll j = 0; j < 100001; ++j) {
			if (j >= p[i][1]) dp[i][j] = max(dp[i/2][j], dp[i/2][j-p[i][1]] + p[i][0]);
			else dp[i][j] = dp[i/2][j];
		}
		//for (ll j = 0; j < 10; ++j) cout << dp[i][j];
		//cout << "\n";
	}
	ll Q; cin >> Q;
	for (ll q = 0; q < Q; ++q) {
		ll x, l; cin >> x >> l;
		if (x < (1<<k)) {
			cout << dp[x][l] << "\n";
			continue;
		}
		ll par = x; ll cnt = 0;
		map<ll, ll> pars;
		while (par >= (1<<k)) {
			pars[(1<<cnt)] = par;
			par /= 2;
			cnt ++;
		}
		ll ans = p[par][l];
		vector<vector<ll> > dp2(1<<cnt, vector<ll>(2, 0));
		for (ll i = 1; i < (1<<cnt); ++i) {
			ll lsb = i & (-i);
			dp2[i][0] = dp2[i-lsb][0] + p[pars[lsb]][0];
			dp2[i][1] = dp2[i-lsb][1] + p[pars[lsb]][1];
			if (dp2[i][1] <= l) {
				ans = max(ans, dp2[i][0] + p[par][l - dp2[i][1]]);
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:14:16: error: no matching function for call to 'min(ll&, int)'
   14 |         k = min(k, 9);
      |             ~~~^~~~~~
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:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:14:16: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   14 |         k = min(k, 9);
      |             ~~~^~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:14:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   14 |         k = min(k, 9);
      |             ~~~^~~~~~