Problem:
Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

3
1 2
2 3
3 4
3
1 1
2 5
3 5

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#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        101005
#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[(1LL<<10)+5][maxn];
vector<int>val(1LL<<19),w(1LL<<19);

int solve2(int v,int mx){
    if(v < min(n+1,1LL<<10)){
        return dp[v][mx];
    }
    int ans = solve2(v/2,mx);
    if(w[v] <= mx)ans = max(ans,solve2(v/2,mx-w[v]));
    return ans;
}

void solve(){
    cin >> n;
    FOR(i,1,n+1)cin >> val[i] >> w[i];
    FOR(i,1,min(n+1,1LL<<10)){
        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;
        cout << solve2(v,mx) << "\n";
    }   
}   
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 ...