Language: C++

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll modinv(ll a, ll mod){
  return modpow(a, mod-2, mod);
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

struct UnionFind{
  ll N;
  llvec p;
  llvec cnt;
  UnionFind(ll n){
    N = n;
    p=llvec(N);
    cnt=llvec(N, 0);
    rep(i, N){
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b){
    P at = root(a);
    P bt = root(b);
    if(at.second!=bt.second){
      if(at.first>bt.first){
        swap(at, bt);
        swap(a, b);
      }
      p[at.second] = bt.second;
      cnt[bt.second]+=cnt[at.second];
      p[a]=bt.second;
    }
  }

  P root(ll a){
    ll atmp = a;
    ll c=0;
    while(atmp!=p[atmp]){
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b){
    P at=root(a);
    P bt=root(b);
    return at.second == bt.second;
  }
  
};

struct dijkstra{
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n){
    N = n;
    //d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost){
    e[from].push_back({to, cost});
  }

  void run(ll start){
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start]=0;
    while(!que.empty()){
      P q = que.top();que.pop();
      ll dc = q.first;
      ll x = q.second;
      if(dc>d[x]){
        continue;
      }else{
        for(auto ip: e[x]){
          if(d[ip.x]<=d[x]+ip.c){
            continue;
          }else{
            d[ip.x]= d[x]+ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }  
};
ll rekkyo(llvec v, llvec w, ll L){
  ll n = v.size();
  ll ans = 0;
  rep(i, 1<<n){
    ll curw=0;
    ll curv=0;
    rep(j, n){
      if(i>>j&1){
        curv += v[j];
        curw += w[j];
      }
      if(curW>L)break;
    }
    if(curw<=L) ans = max(ans, curv);
  }
  return ans;
}
vector<llvec> D;

ll rekkyo2(llvec v, llvec w, ll L, ll k){
  ll n = v.size();
  ll ans = 0;
  rep(i, 1<<n){
    ll curw=0;
    ll curv=0;
    rep(j, n){
      if(i>>j&1){
        curv += v[j];
        curw += w[j];
      }
      if(curW>L)break;
    }
    if(curw<=L)ans = max(ans, curv + D[k][L-curw]);
  }
  return ans;
}

ll dp_eval(llvec V, llvec W, ll L){
  ll N = V.size();
  vector<llvec> dp(N+1, llvec(L+1, 0));
  rep(i, N){
    rep(j, L+1){
      dp[i+1][j]=max(dp[i][j], dp[i+1][j]);
      if(j+W[i]<=L){
        dp[i+1][j+W[i]] = max(dp[i+1][j+W[i]], dp[i][j]+ V[i]);
      }
    }
  }
  return dp[N][L];
}

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N;
  cin >> N;
  llvec V(N+1), W(N+1);
  ll K=9;
  rep(i, N){
    cin >> V[i+1] >> W[i+1];
  }
  D=vector<llvec>((1<<K)+1, llvec(100001, 0));
  rep(j, 100001){
    ll m = min((ll)1<<K, N);
    FOR(i,0, m+1){
      ll a = i;
      ll L = j;
      llvec v, w;
      while(a!=0){
        v.push_back(V[a]);
        w.push_back(W[a]);
        a = a/2;
      }
      D[i][j]=rekkyo(v, w, j);
    }
  }
  ll Q;
  cin >> Q;
  rep(i, Q){
    ll a, L;
    cin >> a >> L;
    if(a<=(1<<K)){
      cout << D[a][L]<<endl;
    }else{
      llvec v, w;
      while(a>(1<<K)){
        v.push_back(V[a]);
        w.push_back(W[a]);
        a = a/2;
      }
      cout << rekkyo2(v, w, L, a);
      cout << endl;
    }
  }
  return 0;
}


Compiler output:
a.cc: In function 'll rekkyo(llvec, llvec, ll)':
a.cc:159:10: error: 'curW' was not declared in this scope; did you mean 'curw'?
  159 |       if(curW>L)break;
      |          ^~~~
      |          curw
a.cc: In function 'll rekkyo2(llvec, llvec, ll, ll)':
a.cc:178:10: error: 'curW' was not declared in this scope; did you mean 'curw'?
  178 |       if(curW>L)break;
      |          ^~~~
      |          curw