Language: C++

#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
//printf("%.10f\n", n);
signed main() {
	//https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_d
	ll n, q, u, l;
	cin >> n; vector<ll> itemv(n + 5), itemw(n + 5);
	for (int h = 1; h <= n; h++) {
		cin >> itemv[h] >> itemw[h];
	}
	vector<vector<ll>> kari(pow(2, 9) + 5, vector<ll>(2));
	vector<vector<ll>> ok1(pow(2, 9) + 5, vector<ll>(2)), ok2(pow(2, 9) + 5, vector<ll>(2));
	cin >> q;
	for (int h = 0; h < q; h++) {
		cin >> u >> l; vector<ll> v, w; ll cnt = 0, ans = 0;
		while (true) {
			v.push_back(itemv[u]); w.push_back(itemw[u]); cnt++;
			if (u == 1) { break; }
			u /= 2;
		}
		n = cnt; cnt = 0;

		if (n % 2 == 0) {


			for (int i = 0; i <= (1 << n / 2); i++) {
				vector<ll> sep = { };
				for (int j = 0; j < n / 2; j++) {
					if ((i & (1 << j)) > 0) {
						sep.push_back(j);
					}
				}
				ll a = 0, b = 0;
				for (ll h = 0; h < sep.size(); h++) {
					a += v[sep[h]]; b += w[sep[h]];
				}
				kari[i][0] = b; kari[i][1] = a; cnt++;*/
			}
			/*sort(kari.begin(), kari.begin() + cnt); ll x = 0, qwerty = 0;
			for (int h = 0; h < cnt; h++) {
				if (h == 0) { ok1[x][0] = kari[h][1]; ok1[x][1] = kari[h][0]; x++; }
				else if (qwerty <= kari[h][1] && kari[h][0] <= l) {
					ok1[x][0] = kari[h][1]; ok1[x][1] = kari[h][0]; x++; qwerty = kari[h][1];
				}
			}
			cnt = 0;
			for (int i = 0; i <= (1 << n / 2); i++) {
				vector<ll> sep = { };
				for (int j = 0; j < n / 2; j++) {
					if ((i & (1 << j)) > 0) {
						sep.push_back(j + n / 2);
					}
				}
				ll a = 0, b = 0;
				for (ll h = 0; h < sep.size(); h++) {
					a += v[sep[h]]; b += w[sep[h]];
				}
				kari[i][0] = b; kari[i][1] = a; cnt++;
			}
			sort(kari.begin(), kari.begin() + cnt); ll y = 0; qwerty = 0;
			for (int h = 0; h < cnt; h++) {
				if (h == 0) { ok2[y][0] = kari[h][1]; ok2[y][1] = kari[h][0]; y++; }
				else if (qwerty <= kari[h][1]) {
					ok2[y][0] = kari[h][1]; ok2[y][1] = kari[h][0]; y++; qwerty = kari[h][1];
				}
			}
			//cout << 222 << ' ' << y << endl;
			for (int h = 0; h < x; h++) {
				ll ok = -1, ng = y, mid = (ok + ng) / 2;
				while (ng - ok > 1) {
					//cout << h << ' ' << ok << ' ' << ng << ' ' << mid << endl;
					if (ok1[h][1] + ok2[mid][1] <= l) { ok = mid; }
					else { ng = mid; }
					mid = (ok + ng) / 2;
				}
				if (ok != -1) { ans = max(ans, ok1[h][0] + ok2[ok][0]); }
			}
			cout << ans << endl;


		}
		/*else {


			n++;
			for (int i = 0; i <= (1 << n / 2); i++) {
				vector<ll> sep = { };
				for (int j = 0; j < n / 2; j++) {
					if ((i & (1 << j)) > 0) {
						sep.push_back(j);
					}
				}
				ll a = 0, b = 0;
				for (ll h = 0; h < sep.size(); h++) {
					//cout << i << ' ' << sep[h] << endl;
					a += v[sep[h]]; b += w[sep[h]];
				}
				kari[i][0] = b; kari[i][1] = a; cnt++;
			}
			sort(kari.begin(), kari.begin() + cnt); ll x = 0;
			for (int h = 0; h < cnt; h++) {
				if (h == 0) { ok1[x][0] = kari[h][1]; ok1[x][1] = kari[h][0]; x++; }
				else if (kari[h - 1][1] <= kari[h][1]) {
					ok1[x][0] = kari[h][1]; ok1[x][1] = kari[h][0]; x++;
				}
			}
			cnt = 0; n--;
			for (int i = 0; i <= (1 << n / 2); i++) {
				vector<ll> sep = { };
				for (int j = 0; j < n / 2; j++) {
					if ((i & (1 << j)) > 0) {
						sep.push_back(j + n / 2 + 1);
					}
				}
				ll a = 0, b = 0;
				for (ll h = 0; h < sep.size(); h++) {
					//cout << i << ' ' << sep[h] << endl;
					a += v[sep[h]]; b += w[sep[h]];
				}
				kari[i][0] = b; kari[i][1] = a; cnt++;
			}
			sort(kari.begin(), kari.begin() + cnt); ll y = 0;
			for (int h = 0; h < cnt; h++) {
				if (h == 0) { ok2[y][0] = kari[h][1]; ok2[y][1] = kari[h][0]; y++; }
				else if (kari[h - 1][1] <= kari[h][1]) {
					ok2[y][0] = kari[h][1]; ok2[y][1] = kari[h][0]; y++;
				}
			}
			//cout << 222 << ' ' << y << endl;
			for (int h = 0; h < x; h++) {
				ll ok = -1, ng = y, mid = (ok + ng) / 2;
				while (ng - ok > 1) {
					//cout << h << ' ' << ok << ' ' << ng << ' ' << mid << endl;
					if (ok1[h][1] + ok2[mid][1] <= l) { ok = mid; }
					else { ng = mid; }
					mid = (ok + ng) / 2;
				}
				if (ok != -1) { ans = max(ans, ok1[h][0] + ok2[ok][0]); }
			}
			cout << ans << endl;


		}*/
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:40:72: error: expected primary-expression before '/' token
   40 |                                 kari[i][0] = b; kari[i][1] = a; cnt++;*/
      |                                                                        ^
a.cc:41:25: error: expected primary-expression before '}' token
   41 |                         }
      |                         ^
a.cc:149:2: error: expected '}' at end of input
  149 | }
      |  ^
a.cc:7:15: note: to match this '{'
    7 | signed main() {
      |               ^