Language: C++

#include<bits/stdc++.h>
#define R(n)for(L i;i<n;i++)
using namespace std;
typedef long long ll;
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define SIZE(x) ((ll)(x).size())
#define PB push_back
#define F first
#define S second

ll n;vector<vector<ll>> vw;vector<vector<ll>> dp;
ll c1,c2;

void dfs(ll i){
    if(i==1){
        dp[i-1][vw[i-1][1]]=vw[i-1][0];
    }else{
        ll j=i/2;
        FORD(k,c2,0){
            if(dp[j-1][k]!=0 or k==0){
                ll ne=k+vw[i-1][1];
                if(ne<=c2)dp[i-1][ne]=max(dp[j-1][k]+vw[i-1][0],dp[i-1][ne]);
            }
            dp[i-1][k]=dp[j-1][k];
        }
    }
    if(i*2<=c1 and i*2<=n)dfs(i*2);
    if(i*2+1<=c1 and i*2+1<=n)dfs(i*2+1);
}

signed main(){
    cin>>n;
    vw.resize(n);
    R(i,n)cin>>vw[i][0]>>vw[i][1];
    c1=1<<10;c2=100000;
    dp.resize(c1);R(i,c1)dp[i].resize(c2+1);
    dfs(1);//cout << 1 << endl;
    R(i,c1)R(j,c2)dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
    ll q;cin>>q;
    R(i,q){
        ll v,l;cin>>v>>l;
        vector<ll> cand;
        while(v>c1){
            cand.PB(v);
            v=v/2;
        }
        ll r=SIZE(cand);//cout << r << endl;
        ll ans=0;
        R(j,1<<r){
            ll v_sub=0;ll w_sub=0;
            R(k,r){
                if((j>>k)&1){
                    v_sub+=vw[cand[k]-1][0];
                    w_sub+=vw[cand[k]-1][1];
                }
            }
            if(w_sub<=l)ans=max(ans,dp[v-1][l-w_sub]+v_sub);
        }
        cout<<ans<<endl;
    }
}

Compiler output:
a.cc:34:10: error: macro "R" passed 2 arguments, but takes just 1
   34 |     R(i,n)cin>>vw[i][0]>>vw[i][1];
      |          ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:36:25: error: macro "R" passed 2 arguments, but takes just 1
   36 |     dp.resize(c1);R(i,c1)dp[i].resize(c2+1);
      |                         ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:38:11: error: macro "R" passed 2 arguments, but takes just 1
   38 |     R(i,c1)R(j,c2)dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
      |           ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:38:18: error: macro "R" passed 2 arguments, but takes just 1
   38 |     R(i,c1)R(j,c2)dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
      |                  ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:40:10: error: macro "R" passed 2 arguments, but takes just 1
   40 |     R(i,q){
      |          ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:49:17: error: macro "R" passed 2 arguments, but takes just 1
   49 |         R(j,1<<r){
      |                 ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc:51:18: error: macro "R" passed 2 arguments, but takes just 1
   51 |             R(k,r){
      |                  ^
a.cc:2:9: note: macro "R" defined here
    2 | #define R(n)for(L i;i<n;i++)
      |         ^
a.cc: In function 'int main()':
a.cc:34:5: error: 'R' was not declared in this scope
   34 |     R(i,n)cin>>vw[i][0]>>vw[i][1];
      |     ^
a.cc:36:20: error: expected ';' before 'dp'
   36 |     dp.resize(c1);R(i,c1)dp[i].resize(c2+1);
      |                    ^     ~~
      |                    ;
a.cc:38:6: error: expected ';' before 'R'
   38 |     R(i,c1)R(j,c2)dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
      |      ^     ~
      |      ;
a.cc:40:6: error: expected ';' before '{' token
   40 |     R(i,q){
      |      ^    ~
      |      ;