Language: C++

#include<utility>
 
using namespace std;
 
int n;
int q;
int v[1<<18], w[1<<18];
vector<pair<int, int>> dp[1<<18];
 
void solve(int u) {
    if (!dp[u].empty()) {
      return;
    }
    if (dp[u/2].empty()) {
      solve(u/2);
    }
    for (pair<int, int> pii: dp[u/2]) {
      int ww = pii.first;
      int vv = pii.second;
      dp[u].push_back(pii);
      dp[u].push_back({ww + w[u], vv + v[u]});
    }
    sort(dp[u].begin(), dp[u].end());
    for (int i = 1; i < dp[u].size(); i++) {
      if (dp[u][i].second < dp[u][i-1].second) {
        dp[u][i].second = dp[u][i-1].second;
      }
    }
}
 
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v[i], &w[i]);
  }
  dp[1].push_back({0, 0});
  dp[1].push_back({w[1], v[1]});
  for (int i = 1; i < n && i < (1 << 10); i++) {
    solve(i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d %d", &u, &l);
    if (u < (1 << 10)) {
      auto it = lower_bound(dp[u].begin(), dp[u].end(), make_pair(l+1, 0));
      printf("%d\n", it - dp[u].begin());
      if (it == dp[u].begin()) {
        printf("0\n");
      } else {
        printf("%d\n", (--it) -> second);
      }
    } else {
      vector<pair<int, int>> brute_force;
      brute_force.push_back({0, 0});
      brute_force.push_back({w[u], v[u]});
      int k = fxxk(u/2, brute_force);
      sort(brute_force.begin(), brute_force.end());
      int l = brute_force.size();
      int p1 = dp[k].size() - 1;
      int p2 = 0;
      int ans = 0;
      while(p2 < l && p1 >= 0) {
        if (dp[k][p1].first + brute_force[p2].first > l) {
          --p1;
        } else {
          ans = max(ans, dp[k][p1].second + brute_force[p2].second);
          ++p2;
        }
      }
      printf("%d\n", ans);
    }
  }
}

Compiler output:
a.cc:8:1: error: 'vector' does not name a type
    8 | vector<pair<int, int>> dp[1<<18];
      | ^~~~~~
a.cc: In function 'void solve(int)':
a.cc:11:10: error: 'dp' was not declared in this scope
   11 |     if (!dp[u].empty()) {
      |          ^~
a.cc:14:9: error: 'dp' was not declared in this scope
   14 |     if (dp[u/2].empty()) {
      |         ^~
a.cc:17:30: error: 'dp' was not declared in this scope
   17 |     for (pair<int, int> pii: dp[u/2]) {
      |                              ^~
a.cc:23:10: error: 'dp' was not declared in this scope
   23 |     sort(dp[u].begin(), dp[u].end());
      |          ^~
a.cc:23:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   23 |     sort(dp[u].begin(), dp[u].end());
      |     ^~~~
      |     short
a.cc: In function 'int main()':
a.cc:32:3: error: 'scanf' was not declared in this scope
   32 |   scanf("%d", &n);
      |   ^~~~~
a.cc:36:3: error: 'dp' was not declared in this scope
   36 |   dp[1].push_back({0, 0});
      |   ^~
a.cc:46:17: error: 'lower_bound' was not declared in this scope
   46 |       auto it = lower_bound(dp[u].begin(), dp[u].end(), make_pair(l+1, 0));
      |                 ^~~~~~~~~~~
a.cc:47:7: error: 'printf' was not declared in this scope
   47 |       printf("%d\n", it - dp[u].begin());
      |       ^~~~~~
a.cc:2:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    1 | #include<utility>
  +++ |+#include <cstdio>
    2 | 
a.cc:54:7: error: 'vector' was not declared in this scope
   54 |       vector<pair<int, int>> brute_force;
      |       ^~~~~~
a.cc:2:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    1 | #include<utility>
  +++ |+#include <vector>
    2 | 
a.cc:54:27: error: expected primary-expression before '>' token
   54 |       vector<pair<int, int>> brute_force;
      |                           ^~
a.cc:54:30: error: 'brute_force' was not declared in this scope
   54 |       vector<pair<int, int>> brute_force;
      |                              ^~~~~~~~~~~
a.cc:57:15: error: 'fxxk' was not declared in this scope
   57 |       int k = fxxk(u/2, brute_force);
      |               ^~~~
a.cc:58:7: error: 'sort' was not declared in this scope; did you mean 'short'?
   58 |       sort(brute_force.begin(), brute_force.end());
      |       ^~~~
      |       short
a.cc:67:17: error: 'max' was not declared in this scope
   67 |           ans = max(ans, dp[k][p1].second + brute_force[p2].second);
      |                 ^~~
a.cc:71:7: error: 'printf' was not declared in this scope
   71 |       printf("%d\n", ans);
      |       ^~~~~~
a.cc:71:7: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'