Language: C++

#include <bits/stdc++.h>
 
//#define int         long long
#define uint        unsigned int
#define ld          long double
#define showoff     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define pii         pair<int,int>
#define FOR(i,a,b)  for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>b;--i)
#define f           first
#define se          second
#define maxn        100005
#define all(v)      v.begin(),v.end()
#define sz(x)       (int)x.size()
#define mod         1000000007
#define pqueue      priority_queue<int>
#define pdqueue     priority_queue< int,vector<int> ,greater< int >>
 
using namespace std;
 
 
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
 
// int power(int a,int n)
// {
//     a %= mod;
//     if(n == 1)return a;
//     if(n == 0)return 1;
//     if(n%2)return (a*(power((a*a)%mod,n/2)%mod))%mod;
//     return power((a*a)%mod,n/2)%mod;
// }
// const int inf = (int) 1e18;
 
// int inverse(int x){
//     return power(x,mod-2)%mod;//little fermat....
// }  

int n,q,dp[(1<<13)+5][maxn];
vector<int>val(maxn),w(maxn);
map<pii,int>mp;

int solve2(int v,int mx){
    if(v < (1<<13)){
        return dp[v][mx];
    }
    //if(mp.find({v,mx}) != mp.end())return mp[{v,mx}];
    int ans = solve2(v/2,mx);
    if(w[v] <= mx)ans = max(ans,solve2(v/2,mx-w[v]));
    //mp.insert({{v,mx},ans});
    return ans;
}

void solve(){
    cin >> n;
    FOR(i,1,n+1)cin >> val[i] >> w[i];
    FOR(i,1,min(n+1,1<<13)){
        FOR(j,0,maxn){
            dp[i][j] = dp[i/2][j];
            if(w[i] <= j)dp[i][j] = max(dp[i][j],val[i]+dp[i/2][j-w[i]]);
        }
    }
    cin >> q;
    while(q--){
        int v,mx;
        cin >> v >> mx;
        //mp.clear();
        cout << solve2(v,mx) << "\n";
    }   
    //mp.clear();
}   
signed main()
{
    showoff;
    int T = 1;
    //cin >> T;
    FOR(t,1,T+1){   
        solve();
        //cout << "\n";
    }
    return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...
 

Compiler output:
/tmp/ccxGCKSB.o: in function `solve2(int, int)':
a.cc:(.text+0x6b): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x8e): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
/tmp/ccxGCKSB.o: in function `solve()':
a.cc:(.text+0x10e): relocation truncated to fit: R_X86_64_PC32 against symbol `val' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x13a): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x1dd): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x204): relocation truncated to fit: R_X86_64_PC32 against symbol `val' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x22c): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
/tmp/ccxGCKSB.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x430): relocation truncated to fit: R_X86_64_PC32 against symbol `val' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x456): relocation truncated to fit: R_X86_64_PC32 against symbol `val' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x485): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccxGCKSB.o
a.cc:(.text+0x4ab): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status