Problem p02648 (python)

Fixed submission:
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 or 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<<11)
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
    ctr = 0
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ctr = max(ctr, kv+Rv[v][cnt])
    ans[i] = ctr

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline


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

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    N = len(Av)
    resv, resw = [None]*(2*N), [None]*(2*N)

    idx = 0
    jdx = 0
    for i in range(2*N):
        if jdx == N or Aw[idx] <= Aw[jdx] - w:
            resv[i] = Av[idx]
            resw[i] = Aw[idx]
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv[i] = Av[jdx] + v
            resw[i] = 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
    ctr = 0
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ctr = max(ctr, kv+Rv[v][cnt])
    ans[i] = ctr

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

Buggy submission (verdict: Runtime Error):
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
        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
        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<<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:])))

Buggy submission (verdict: Runtime Error):
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:
            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
        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:])))

Buggy submission (verdict: Runtime Error):
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]:
            acnt -= 1
        ans[i] = max(ans[i], kv+Rv[v][cnt])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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<<12)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])
    
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]
    Kv, Kw = [0], [l]
    while v > BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    tv[v].append(Kv)
    tw[v].append(Kw)
    ti[v].append([i]*len(Kv))
    
for i in range(1, N+1):
    lt = len(tv[i])
    if lt == 0:
        continue
    if lt == 1:
        tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]
        continue
    d = 1
    while 2*d <= lt:
        for j in range(0, 1+lt-2*d, 2*d):
            tv[i][j], tw[i][j], ti[i][j] = calc(tv[i][j], tw[i][j], ti[i][j], tv[i][j+d], tw[i][j+d], ti[i][j+d])
        if j+2*d < lt:
            tv[i][0], tw[i][0], ti[0][0] = calc(tv[i][0], tw[i][0], ti[i][0], tv[i][j+2*d], tw[i][j+2*d], ti[i][j+2*d])
        d <<= 1
    tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if tv[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(tv[i])-1, -1, -1):
            v, rw, idx = tv[i][k], tw[i][k], ti[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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<<12)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])
    
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]
    Kv, Kw = [0], [l]
    while v > BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    tv[v].append(Kv)
    tw[v].append(Kw)
    ti[v].append([i]*len(Kv))
    
for i in range(1, N+1):
    lt = len(tv[i])
    if lt == 0:
        continue
    if lt == 1:
        tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]
        continue
    d = 1
    while 2*d <= lt:
        for j in range(0, 1+lt//(2*d) * 2*d, 2*d):
            tv[i][j], tw[i][j], ti[i][j] = calc(tv[i][j], tw[i][j], ti[i][j], tv[i][j+d], tw[i][j+d], ti[i][j+d])
        if j+2*d < lt:
            tv[i][0], tw[i][0], ti[0][0] = calc(tv[i][0], tw[i][0], ti[i][0], tv[i][j+2*d], tw[i][j+2*d], ti[i][j+2*d])
        d <<= 1
    tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if tv[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(tv[i])-1, -1, -1):
            v, rw, idx = tv[i][k], tw[i][k], ti[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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<<12)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])
    
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]
    Kv, Kw = [0], [l]
    while v > BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    tv[v].append(Kv)
    tw[v].append(Kw)
    ti[v].append([i]*len(Kv))
    
for i in range(1, N+1):
    lt = len(tv[i])
    if lt == 0:
        continue
    if lt == 1:
        tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]
        continue
    d = 1
    
    while 2*d <= lt:
        for j in range(0, lt, 2*d):
            tv[i][j], tw[i][j], ti[i][j] = calc(tv[i][j], tw[i][j], ti[i][j], tv[i][j+d], tw[i][j+d], ti[i][j+d])
        if j+2*d < lt:
            tv[i][0], tw[i][0], ti[0][0] = calc(tv[i][0], tw[i][0], ti[i][0], tv[i][j+2*d], tw[i][j+2*d], ti[i][j+2*d])
        d <<= 1
    tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if tv[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(tv[i])-1, -1, -1):
            v, rw, idx = tv[i][k], tw[i][k], ti[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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<<12)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])
    
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]
    Kv, Kw = [0], [l]
    while v > BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    tv[v].append(Kv)
    tw[v].append(Kw)
    ti[v].append([i]*len(Kv))
    
for i in range(1, N+1):
    lt = len(tv[i])
    if lt == 0:
        continue
    if lt == 1:
        tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]
        continue
    d = 1
    
    while 2*d <= lt:
        for j in range(0, lt, 2*d):
            tv[i][j], tw[i][j], ti[i][j] = calc(tv[i][j], tw[i][j], ti[i][j], tv[i][j+d], tw[i][j+d], ti[i][j+d])
        if lt-1 <= j+2*d:
            tv[i][0], tw[i][0], ti[0][0] = calc(tv[i][0], tw[i][0], ti[i][0], tv[i][j+2*d], tw[i][j+2*d], ti[i][j+2*d])
        d <<= 1
    tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if tv[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(tv[i])-1, -1, -1):
            v, rw, idx = tv[i][k], tw[i][k], ti[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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 calc1(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<<12)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])
    
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]
    Kv, Kw = [0], [l]
    while v > BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    tv[v].append(Kv)
    tw[v].append(Kw)
    ti[v].append([i]*len(Kv))
    
for i in range(1, N+1):
    lt = len(tv[i])
    if lt == 0:
        continue
    if lt == 1:
        tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]
        continue
    
    d = 1
    
    while 2*d <= lt:
        for j in range(0, lt, 2*d):
            tv[i][j], tw[i][j], ti[i][j] = calc1(tv[i][j], tw[i][j], ti[i][j], tv[i][j+1], tw[i][j+1], ti[i][j+1])
        if lt&1:
            tv[i][0], tw[i][0], ti[0][0] = calc1(tv[i][0], tw[i][0], ti[i][0], tv[i][-1], tw[i][-1], ti[i][-1])
        d <<= 1
    tv[i], tw[i], ti[i] = tv[i][0], tw[i][0], ti[i][0]

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if tv[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(tv[i])-1, -1, -1):
            v, rw, idx = tv[i][k], tw[i][k], ti[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

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

def calc(A, B):
    res = []
    N = len(A)
    M = len(B)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            res.append(B[bdx])
            bdx += 1
            continue
        if bdx == M:
            res.append(A[adx])
            adx += 1
            continue
        if A[adx][1] < B[bdx][1]:
            res.append(A[adx])
            adx += 1
        else:
            res.append(B[bdx])
            bdx += 1
    return res

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

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

table = [[] for _ in range(N+1)]

for i in range(1, Q+1):
    v, l = VL[i]
    K = [(0, l)]
    while v > BS:
        K = mergedown(K, VW[v])
        v >>= 1
    K = [(v, rw, i) for v, rw in K]
    table[v] = calc(table[v], K)    

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if table[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(table[i])-1, -1, -1):
            v, rw, idx = table[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

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