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



Sample input:
3
1 2
2 3
3 4
3
1 1
2 5
3 5


Sample output:
0
3
3

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline


def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<9)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(BS+1)]
Rw = [[0] for _ in range(BS+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
          Rv[i].append(vm)
          Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import os
from io import BytesIO
readline = BytesIO(os.read(0, os.fstat(0).st_size)).readline


def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(BS+1)]
Rw = [[0] for _ in range(BS+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
          Rv[i].append(vm)
          Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import os
from io import BytesIO
readline = BytesIO(os.read(0, os.fstat(0).st_size)).readline


def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw

if __name__ == '__main__':
    limit = 10**5
    N = int(readline())
    VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
    BS = min(N, 1<<10)
    Q = int(readline())
    VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]
    
    
    Rv = [[0] for _ in range(BS+1)]
    Rw = [[0] for _ in range(BS+1)]
    for i in range(1, BS):
        for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
            if Rv[i][-1] < vm:
              Rv[i].append(vm)
              Rw[i].append(wm)
        
    ans = [0]*(Q+1)
    for i in range(1, Q+1):
        v, l = VL[i]
        Kv, Kw = [0], [l]
        while v >= BS:
            Kv, Kw = mergedown(Kv, Kw, VW[v])
            v >>= 1
        cnt = len(Rv[v])-1
        for j in range(len(Kv)-1, -1, -1):
            kv, kw = Kv[j], Kw[j]
            while kw < Rw[v][cnt]:
                cnt -= 1
            ans[i] = max(ans[i], kv+Rv[v][cnt])
    
    print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw

N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(BS+1)]
Rw = [[0] for _ in range(BS+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
          Rv[i].append(vm)
          Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw

if __name__ == '__main__':
    N = int(readline())
    VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
    BS = min(N, 1<<10)
    Q = int(readline())
    VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]
    
    
    Rv = [[0] for _ in range(BS+1)]
    Rw = [[0] for _ in range(BS+1)]
    for i in range(1, BS):
        for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
            if Rv[i][-1] < vm:
              Rv[i].append(vm)
              Rw[i].append(wm)
        
    ans = [0]*(Q+1)
    for i in range(1, Q+1):
        v, l = VL[i]
        Kv, Kw = [0], [l]
        while v >= BS:
            Kv, Kw = mergedown(Kv, Kw, VW[v])
            v >>= 1
        cnt = len(Rv[v])-1
        for j in range(len(Kv)-1, -1, -1):
            kv, kw = Kv[j], Kw[j]
            while kw < Rw[v][cnt]:
                cnt -= 1
            ans[i] = max(ans[i], kv+Rv[v][cnt])
    
    print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw

if __name__ == '__main__':
    limit = 10**5
    N = int(readline())
    VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
    BS = min(N, 1<<10)
    Q = int(readline())
    VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]
    
    
    Rv = [[0] for _ in range(BS+1)]
    Rw = [[0] for _ in range(BS+1)]
    for i in range(1, BS):
        for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
            if Rv[i][-1] < vm:
              Rv[i].append(vm)
              Rw[i].append(wm)
        
    ans = [0]*(Q+1)
    for i in range(1, Q+1):
        v, l = VL[i]
        Kv, Kw = [0], [l]
        while v >= BS:
            Kv, Kw = mergedown(Kv, Kw, VW[v])
            v >>= 1
        cnt = len(Rv[v])-1
        for j in range(len(Kv)-1, -1, -1):
            kv, kw = Kv[j], Kw[j]
            while kw < Rw[v][cnt]:
                cnt -= 1
            ans[i] = max(ans[i], kv+Rv[v][cnt])
    
    print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(BS+1)]
Rw = [[0] for _ in range(BS+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
          Rv[i].append(vm)
          Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(N+1)]
Rw = [[0] for _ in range(N+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
          Rv[i].append(vm)
          Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [None]*(N+1)
Rw = [None]*(N+1)
Rv[0] = [0]
Rw[0] = [0]
for i in range(1, BS):
    rv, rw = [0], [0]
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if rv[-1] < vm:
          rv.append(vm)
          rw.append(wm)
    Rv[i], Rw[i] = rv[:], rw[:]
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw

N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [None]*(N+1)
Rw = [None]*(N+1)
Rv[0] = [0]
Rw[0] = [0]
for i in range(1, BS):
    Rv[i], Rw[i] = mergeup(Rv[i//2], Rw[i//2], VW[i])
    rv, rw = [Rv[i][0]], [Rw[i][0]]
    for j in range(1, len(Rv[i])):
        if rv[-1] < Rv[i][j]:
          rv.append(Rv[i][j])
          rw.append(Rw[i][j])
    Rv[i], Rw[i] = rv[:], rw[:]
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
            continue
        if Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<10)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [None]*(N+1)
Rw = [None]*(N+1)
Rv[0] = [0]
Rw[0] = [0]
for i in range(1, BS):
    Rv[i], Rw[i] = mergeup(Rv[i//2], Rw[i//2], VW[i])
    rv, rw = [Rv[i][0]], [Rw[i][0]]
    for j in range(1, len(Rv[i])):
        if rv[-1] < Rv[i][j]:
          rv.append(Rv[i][j])
          rw.append(Rw[i][j])
    Rv[i], Rw[i] = rv[:], rw[:]
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

print('\n'.join(map(str, ans[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter
readline = sys.stdin.readline

def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, Ai, vw):
    v, w = vw
    resv, resw, resi = [], [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N or Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            resi.append(Ai[idx])
            idx += 1
            continue
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            resi.append(Ai[jdx])
            jdx += 1
    return resv, resw, resi

def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]

BS = min(N+1, 1<<13)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]

Rv = [None]*(N+1)
Rw = [None]*(N+1)
Rv[0] = [0]
Rw[0] = [0]
for i in range(1, BS):
    Rv[i], Rw[i] = mergeup(Rv[i//2], Rw[i//2], VW[i])

table = [[] for _ in range(N+1)]
tv = [[] for _ in range(N+1)]
tw = [[] for _ in range(N+1)]
ti = [[] for _ in range(N+1)]
for i in range(1, Q+1):
    v, l = VL[i]
    table[v].append((0, l, i))
for i in range(1, N+1):
    if table[i]:    
        table[i].sort(key = itemgetter(1))
        tv[i], tw[i], ti[i] = map(list, zip(*table[i]))

cnt = 0
for i in range(N, BS-1, -1):
    if tv[i]:
        cnt += 2*len(tv[i])
        tv[i//2], tw[i//2], ti[i//2] = calc(*mergedown(tv[i], tw[i], ti[i], VW[i]), tv[i//2], tw[i//2], ti[i//2])

ans = [0]*(Q+1)
for i in range(1, BS):
    if tv[i]:
        rv = [Rv[i][0]]
        rw = [Rw[i][0]]
        for j in range(1, len(Rv[i])):
            if rv[-1] < Rv[i][j]:
                rv.append(Rv[i][j])
                rw.append(Rw[i][j])
        for k in range(len(tv[i])-1, -1, -1):
            v, rew, idx = tv[i][k], tw[i][k], ti[i][k]
            while rw[-1] > rew:
                rw.pop()
                rv.pop()
            ans[idx] = max(ans[idx], v+rv[-1])
print('\n'.join(map(str, ans[1:])))