Language: C++

#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>
using namespace std;
const int size=40;
int amount;
vector<vector<int>> tree(size, vector<int>(1));
vector<tuple<int, int, int>> values(size);
vector<tuple<int, int>> weights(size);
vector<tuple<int, int, int>> selectedValues(size);
vector<int> sumValues(size);
void fillingValues()
{
	int pointValue, pointWeight;
	cin>>amount;
	for(int point=1; point<=amount; ++point)
	{
		cin>>pointValue>>pointWeight;
		values.push_back(make_tuple(point, pointValue, pointWeight));
	}
	return;
}
void fillingWeights()
{
	int borderPoint, borderWeight, queryNumbers;
	cin>>queryNumbers;
	for(int i=1; i<=queryNumbers; ++i)
	{
		cin>>borderPoint>>borderWeight;
		weights.push_back(make_tuple(borderPoint, borderWeight));
	}
	return;
}
void creatingTree(int root)
{
	if(root>amount)
	{
		return;
	}
	tree[root].push_back(2*root);
	tree[root].push_back(2*root+1);
	tree[2*root].push_back(root);
	tree[2*root+1].push_back(root);
	creatingTree(2*root);
	creatingTree(2*root+1);
	return;
}
int rootWeight=-1, rootValue=-1;
void dfs(int root, int borderWeight)
{
	if(root==0)
	{
		cout<<(*max_element(sumValues.begin(), sumValues.end()))<<endl;
		return;
	}
	for(int i=0; i<values.size(); ++i)
	{
		if((get<0>(values[i]))==root)
		{
			rootValue=get<1>(values[i]);
			rootWeight=get<2>(values[i]);
		}
	}
	int rootCopy=root;
	while(rootCopy!=0)
	{
		for(int i=0; i<values.size(); ++i)
		{
			if(get<0>(values[i])==rootCopy/2)
			{
				if((rootWeight+get<2>(values[i]))<=borderWeight)
				{
					sumValues.push_back(rootValue+get<1>(values[i]));
				}
			}
		}
		rootCopy=(rootCopy/2);
	}
	for(int first : tree[root])
	{
		if(first==root/2)
		{
			dfs(first, borderWeight);
		}
	}
	return;
}
void usingDfs()
{
	for(int i=0; i<weights.size(); ++i)
	{
		dfs(get<0>(weights[i]), get<1>(weights[i]));
		sumValues.clear();
		selectedValues.clear();
	}
	//dfs(12, 240);
	return;
}
template<typename Type>
void showContent2D(Type input)
{
	for(int i=1; i<input.size(); ++i)
	{
		for(int j=0; j<input[i].size(); ++j)
		{
			if(input[i][j]>0)
			{
				cout<<"["<<i<<"]["<<j<<"] <- "<<input[i][j]<<endl;
			}
		}
	}
	return;
}
template<typename Type>
void showContentTuple3(Type input)
{
	for(int i=0; i<input.size(); ++i)
	{
		cout<<get<0>(input[i])<<" "<<get<1>(input[i])<<" "<<get<2>(input[i])<<endl;
	}
	return;
}
template<typename Type>
void showContentTuple2(Type input)
{
	for(int i=0; i<input.size(); ++i)
	{
		cout<<get<0>(input[i])<<" "<<get<1>(input[i])<<endl;
	}
	return;
}
int main()
{
	fillingValues();
	fillingWeights();
	creatingTree(1);
	usingDfs();
	return 0;
}

Compiler output:
a.cc:8:26: error: reference to 'size' is ambiguous
    8 | vector<vector<int>> tree(size, vector<int>(1));
      |                          ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:11: note:                 'const int size'
    6 | const int size=40;
      |           ^~~~
a.cc:9:37: error: reference to 'size' is ambiguous
    9 | vector<tuple<int, int, int>> values(size);
      |                                     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:11: note:                 'const int size'
    6 | const int size=40;
      |           ^~~~
a.cc:10:33: error: reference to 'size' is ambiguous
   10 | vector<tuple<int, int>> weights(size);
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:11: note:                 'const int size'
    6 | const int size=40;
      |           ^~~~
a.cc:11:45: error: reference to 'size' is ambiguous
   11 | vector<tuple<int, int, int>> selectedValues(size);
      |                                             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:11: note:                 'const int size'
    6 | const int size=40;
      |           ^~~~
a.cc:12:23: error: reference to 'size' is ambiguous
   12 | vector<int> sumValues(size);
      |                       ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:11: note:                 'const int size'
    6 | const int size=40;
      |           ^~~~