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: Time Limit Exceeded)

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
typedef pair<int, int> P;
int v[20], w[20];
int L, N;
int f1() {
    vector<P> v1, v2;
    int n1 = N*3/5;
    int n2 = N - n1;
    rep(bit, (1<<n1)) {
        int sum_v = 0, sum_w = 0;
        rep(i, n1) {
            if (bit & (1 << i)) {
                sum_v += v[i];
                sum_w += w[i];
            }
        }
        //cout << bit << " " << sum_w << " " << sum_v << endl;
       v1.push_back({sum_w, sum_v});
    }
    rep(bit, (1<<n2)) {
        int sum_v = 0, sum_w = 0;
        rep(i, n2){
            if (bit & (1 << i)) {
                sum_v += v[i + n1];
                sum_w += w[i + n1];
            }
        }
        v2.push_back({sum_w, sum_v});
    }
    sort(v2.begin(), v2.end());
    vector<P> v3;
    int max_v = -1;
    rep(i, v2.size()) {
        if (v2[i].second > max_v) {
            v3.push_back(v2[i]);
            max_v = v2[i].second;
        }
    }
    v3.push_back({INF2, INF2});
    int ans = 0;
    // rep(i, v1.size()) {
    //     cout << "*" << v1[i].first << " " << v1[i].second << endl;
    // }
    // rep(i, v3.size()) {
    //     cout << "**" << v3[i].first << " " << v3[i].second << endl;
    // }
    
    rep(i, v1.size()) {
        int sum_v = v1[i].second;
        int remain_w = L - v1[i].first;
        if (remain_w < 0) continue;
        //cout << remain_w << " "<< sum_v << " ";
        auto itr = upper_bound(v3.begin(), v3.end(), make_pair(remain_w, INF2), less<P>());
        itr--;
        sum_v += itr->second;
        //cout << sum_v << endl;
        ans = max(ans, sum_v);
    }
 
    return ans;
}
signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //std::ifstream in("test.txt");
    //std::cin.rdbuf(in.rdbuf());
    int n; cin >> n; 
    int V[300010], W[300010];
    rep(i, n) {
        cin >> V[i] >> W[i];
    }
    int Q; cin >> Q;
    int sum = 0;
    while(Q--) {
        int tmp_v;
        cin >> tmp_v >> L;
        N = 1;
        while(true) {
            v[N - 1] = V[tmp_v - 1];
            w[N - 1] = W[tmp_v - 1];
            if(tmp_v == 1) break;
            tmp_v /= 2;
            N++;
        }
        //cout << "*" << N << " " << L << " " << v[0] << " " << w[0] << endl;
        //if(Q % 10000 == 0) cout << f1() << endl;
        //else sum += f1();
        cout << f1() << endl;
    }
    //cout << sum << endl;

    return 0;
}