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: Runtime Error)

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,q;
//2つの要素をまとめてソート、前の変数が優先。flag=0で小さい順、1で大きい順
void mysort2(vi &a,vi &b,ll flag){
  if (a.size()>0){
    vector<pair<ll, ll>> p(a.size());
    REP(i,a.size()){
      p[i].first=a[i];
      p[i].second=b[i];
    }
    sort(p.begin(),p.end());
    if (flag==1){
      reverse(p.begin(),p.end());
    }
    REP(i,a.size()){
      a[i]=p[i].first;
      b[i]=p[i].second;
    }
  }
  return;
}


int main(){
  ll ans=0;
  cin >> n;
  ll nlog=0;//木の高さ
  for(ll i=0;;){
    i=(i+1LL)*2LL-1LL;
    nlog++;
    if (i>=n)break;
  }


  ll nhi=nlog/2;
  ll nlo=nlog-nhi;

  vi v(n),w(n);
  REP(i,n){
    cin>>v[i]>>w[i];
  }

  cin >> q;
  vi u(q),L(q);
  REP(i,q){
    cin>>u[i]>>L[i];
    u[i]--;
  }

  


  //上の部分だけ最初にDPやっとく
  ll dpnum=1LL<<(nhi-1);
  ll dpww=dpnum*2LL;
  vvi dp(dpnum, vi(dpnum*2LL));
  vi weit(dpnum*2LL);//重さ変数
  vi valu(dpnum*2LL);//価値変数
  ll dpi=dpnum-1LL;
  for(ll i=0;i<dpnum;i++){
    //1本のweitをソートして保持しておく
    for(ll j=0;j<dpnum*2LL;j++){
      weit[j]=0LL;
      valu[j]=0LL;
      ll iii=dpnum-1LL+i;
      for(ll k=1LL;k<dpnum*2LL;k*=2LL){
        if ((j&k)!=0){
          weit[j]+=w[iii];
          valu[j]+=v[iii];
        }
        iii=(iii+1LL)/2LL-1LL;
      }
    }

    //weitをソートしてまとめる
    mysort2(weit,valu,0LL);
    
    ll vmax=0;//その重さでの価値のmax
    for(ll j=0;j<dpnum*2LL;j++){
      if (vmax<valu[j])vmax=valu[j];
      dp[i][j]=weit[j]*16777216LL+vmax;
    }
    
  }





/////////////////////////////
for(ll loop=0;loop<q;loop++){
  

    ll munlog=0;
    for(ll i=0;;){
      i=(i+1LL)*2LL-1LL;
      munlog++;
      if (i>=(u[loop]+1LL))break;
    }

  //次に下の部分
  if (u[loop]<dpnum){
    //上田家の計算ですむ
    dpnum=1LL<<munlog;
    
    vi weit2(dpnum);//重さ変数
    vi valu2(dpnum);//価値変数
    ll iii;
    for(ll j=0;j<dpnum;j++){
      weit2[j]=0LL;
      valu2[j]=0LL;
      iii=u[loop];
      for(ll k=0;k<munlog;k++){
        if ((j&(1LL<<k))!=0){
          weit2[j]+=w[iii];
          valu2[j]+=v[iii];
        }
        iii=(iii+1LL)/2LL-1LL;
      }
    }
    //weitをソートしてまとめる
    mysort2(weit2,valu2,0LL);
    ll vmax=0;//その重さでの価値のmax
    for(ll j=0;j<dpnum;j++){
      if (vmax<valu2[j]){
        vmax=valu2[j];
      }
      valu2[j]=vmax;
    }
    ll maxvalu=0;
    for(ll j=0;j<dpnum;j++){
      if (weit2[j]>L[loop])break;
      maxvalu=max(maxvalu,valu2[j]);
    }

    cout<<maxvalu<<endl;

  }else{//下の計算をする
    munlog=munlog-nhi;//下の段数
    dpnum=1LL<<munlog;
    
    vi weit2(dpnum);//重さ変数
    vi valu2(dpnum);//価値変数
    ll iii;
    for(ll j=0;j<dpnum;j++){
      weit2[j]=0LL;
      valu2[j]=0LL;
      iii=u[loop];
      for(ll k=0;k<munlog;k++){
        if ((j&(1LL<<k))!=0){
          weit2[j]+=w[iii];
          valu2[j]+=v[iii];
        }
        iii=(iii+1LL)/2LL-1LL;
      }
    }
    ll dpilo=iii-dpi;
    //cout<<dpilo<<" ";

    //weitをソートしてまとめる
    mysort2(weit2,valu2,0LL);
    
    ll vmax=0;//その重さでの価値のmax
    for(ll j=0;j<dpnum;j++){
      if (vmax<valu2[j]){
        vmax=valu2[j];
      }
      valu2[j]=vmax;
    }

    //上の分と比較していく
    ll dpw=dpww-1LL;
    ll maxvalu=0;
    for(ll i=0;i<dpnum;i++){
      ll dw=dp[dpilo][dpw]/16777216LL;
      if (dw+weit2[i]>L[loop]){
        dpw--;
        if (dpw<0)break;
        i--;
        continue;
      }else{//重さが満たしていたら
        maxvalu=max(dp[dpilo][dpw]%16777216LL+valu2[i],maxvalu);
      }
    }

    //答えを出力
    cout<<maxvalu<<endl;
  }//ifの最後
}//loopの最後



  return 0;
}