ZhenlongDai/ACPR huggingface dataset:
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
x, y, a, b, c = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
p.sort(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)
ri = max(0, x - a) + max(0, y - b)
pi = min(a, x)
qi = min(b, y)
while ri < len(r) and (0 < pi or 0 < qi):
    if 0 < pi and p[pi - 1] < r[ri] and (qi <= 0 or p[pi - 1] < q[qi - 1]):
        ri += 1
        pi -= 1
    elif 0 < qi and q[qi - 1] < r[ri] and (pi <= 0 or q[qi - 1] < p[pi - 1]):
        ri += 1
        qi -= 1
    else:
        break
print(sum(p[:pi]) + sum(q[:qi]) + sum(r[:ri]))


Failing test case input:
1 2 -1 2 1
21 0
9 0
2 1

Expected output:
32


Actual output:
33


Fixed submission (verdict: Accepted):
import sys
x, y, a, b, c = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
p.sort(key=lambda x: -x)
q.sort(key=lambda x: -x)
r.sort(key=lambda x: -x)
ri = 0
pi = x
qi = y
while ri < len(r) and (0 < pi or 0 < qi):
    if 0 < pi and p[pi - 1] < r[ri] and (qi <= 0 or p[pi - 1] <= q[qi - 1]):
        ri += 1
        pi -= 1
    elif 0 < qi and q[qi - 1] < r[ri] and (pi <= 0 or q[qi - 1] <= p[pi - 1]):
        ri += 1
        qi -= 1
    else:
        break
print(sum(p[:pi]) + sum(q[:qi]) + sum(r[:ri]))

Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x, y, a, b, c = [int(i) for i in input().split()]
pa = [int(i) for i in input().split()]
pb = [int(i) for i in input().split()]
pc = [int(i) for i in input().split()]

pa = sorted(pa, reverse=True)
pa = pa[:x]
pb = sorted(pb, reverse=True)
pb = pb[:y]
pc = sorted(pc, reverse=True)

l = pa + pb
l = sorted(l, reverse=True)

for i in range(len(pc)):
  for j in range(len(l)):
    if pc[i] > l[j]:
      l[j] = pc[i]
  if j == len(l)-1:
    break
      
print(sum(l))
  

Failing test case input:
2 2 2 2 3
8 1
9 1
2 1

Expected output:
20


Actual output:
21


Fixed submission (verdict: Accepted):
x, y, a, b, c = [int(i) for i in input().split()]
pa = [int(i) for i in input().split()]
pb = [int(i) for i in input().split()]
pc = [int(i) for i in input().split()]

pa = sorted(pa, reverse=True)
pa = pa[:x]
pb = sorted(pb, reverse=True)
pb = pb[:y]
pc = sorted(pc, reverse=True)

l = pa + pb + pc
l = sorted(l, reverse=True)
l = l[:int(x+y)]
print(sum(l))
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x,y,a,b,c = map(int,input().split())
pl = list(map(int,input().split()))
ql = list(map(int,input().split()))
rl = list(map(int,input().split()))

pp = sorted(pl,reverse=True)[:x-1]
qq = sorted(pl,reverse=True)[:y-1]
all = pp + qq + rl
print(sum(sorted(pl,reverse=True)[:x+y-1]))

Failing test case input:
2 2 4 8 1
6 3
1 1
3

Expected output:
13


Actual output:
9


Fixed submission (verdict: Accepted):
import heapq
x,y,a,b,c = map(int,input().split())
pl = list(map(int,input().split()))
ql = list(map(int,input().split()))
rl = list(map(int,input().split()))

pp = sorted(pl,reverse=True)[:x]
qq = sorted(ql,reverse=True)[:y]
al = pp + qq + rl

print(sum(sorted(al,reverse=True)[:x+y]))

Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
X,Y,A,B,C = (int(x) for x in input().split())
p = [[int(x), 'r'] for x in input().split()]
q = [[int(x), 'g'] for x in input().split()]
r = [[int(x), 'c'] for x in input().split()]
pqr = p + q + r
pqr.sort(reverse=True)
d = deque(pqr)
rem = X + Y
picked = 0
ans = 0
while rem > picked:
    pop = d.popleft()
    if pop[1] == 'r':
        if X > 0:
            X -= 1
            ans += pop[0]
            picked += 1
    elif pop[1] == 'g':
        if Y > 0:
            Y -= 1
            ans += pop[0]
            picked += 1
    else:
        rem -= 1
        ans += pop[0]
        picked += 1
print(ans)

Failing test case input:
1 2 8 4 1
0 1
1 1
3

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
X,Y,A,B,C = (int(x) for x in input().split())
p = [[int(x), 'r'] for x in input().split()]
q = [[int(x), 'g'] for x in input().split()]
r = [[int(x), 'c'] for x in input().split()]
pqr = p + q + r
pqr.sort(reverse=True)
d = deque(pqr)
rem = X + Y
pickedr = 0
pickedg = 0
pickedc = 0
ans = 0
if len(d) <= rem:
    print(sum([apple[0] for apple in pqr]))
else:
    while pickedr + pickedg + pickedc < rem:
        pop = d.popleft()
        if pop[1] == 'r':
            if pickedr < X:
                ans += pop[0]
                pickedr += 1
        elif pop[1] == 'g':
            if pickedg < Y:
                ans += pop[0]
                pickedg += 1
        else:
            ans += pop[0]
            pickedc += 1
    print(ans)
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
X,Y,A,B,C = (int(x) for x in input().split())
p = [[int(x), 'r'] for x in input().split()]
q = [[int(x), 'g'] for x in input().split()]
r = [[int(x), 'c'] for x in input().split()]
pqr = p + q + r
pqr.sort(reverse=True)
d = deque(pqr)
rem = X + Y
picked = 0
ans = 0
if len(d) <= rem:
    print(sum([apple[0] for apple in pqr]))
else:
    while rem > picked:
        pop = d.popleft()
        if pop[1] == 'r':
            if X > 0:
                X -= 1
                ans += pop[0]
                picked += 1
        elif pop[1] == 'g':
            if Y > 0:
                Y -= 1
                ans += pop[0]
                picked += 1
        else:
            rem -= 1
            ans += pop[0]
            picked += 1
    print(ans)

Failing test case input:
1 2 8 4 1
0 1
1 1
3

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
X,Y,A,B,C = (int(x) for x in input().split())
p = [[int(x), 'r'] for x in input().split()]
q = [[int(x), 'g'] for x in input().split()]
r = [[int(x), 'c'] for x in input().split()]
pqr = p + q + r
pqr.sort(reverse=True)
d = deque(pqr)
rem = X + Y
pickedr = 0
pickedg = 0
pickedc = 0
ans = 0
if len(d) <= rem:
    print(sum([apple[0] for apple in pqr]))
else:
    while pickedr + pickedg + pickedc < rem:
        pop = d.popleft()
        if pop[1] == 'r':
            if pickedr < X:
                ans += pop[0]
                pickedr += 1
        elif pop[1] == 'g':
            if pickedg < Y:
                ans += pop[0]
                pickedg += 1
        else:
            ans += pop[0]
            pickedc += 1
    print(ans)

Problem: p02727
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
X, Y, a, b, c = list(map(int,input().split()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
s = sorted(p)[::-1][:X] + sorted(q)[::-1][:X] + r
ans = sum(s[:X + Y])
print(ans)                   

Failing test case input:
0 2 4 0 8
5 12 2 1
4 25 95 44
2 1 2 1

Expected output:
139


Actual output:
3


Fixed submission (verdict: Accepted):
X, Y, a, b, c = list(map(int,input().split()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
s = sorted(p)[::-1][:X] + sorted(q)[::-1][:Y] + r
s = sorted(s)[::-1]
ans = sum(s[:X + Y])
print(ans)
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
Z = X+Y
P = p[:X]
Q = q[:Y]
R = r[:Z]
ans = 0
if X <= Y:
  S = P + R
  S.sort(reverse=True)
  a = S[:X]
  b = S[X:]
  ans += sum(a)
  T = Q + b
  T.sort(reverse=True)
  c = T[:Y]
  ans += sum(c)
else:
  S = Q + R
  S.sort(reverse=True)
  a = S[:Y]
  b = S[Y:]
  ans += sum(a)
  T = P + b
  T.sort(reverse=True)
  c = T[:X]
  ans += sum(c)
print(ans)
print(a,b)

Failing test case input:
0 2 5 -2 8
8 5 2 1
4 34 100 44
2 1 4 1

Expected output:
144


Actual output:
144
[] [4, 2]


Fixed submission (verdict: Accepted):
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
Z = X+Y
P = p[:X]
Q = q[:Y]
R = r[:Z]
ans = 0
if X <= Y:
  S = P + R
  S.sort(reverse=True)
  a = S[:X]
  b = S[X:]
  ans += sum(a)
  T = Q + b
  T.sort(reverse=True)
  c = T[:Y]
  ans += sum(c)
else:
  S = Q + R
  S.sort(reverse=True)
  a = S[:Y]
  b = S[Y:]
  ans += sum(a)
  T = P + b
  T.sort(reverse=True)
  c = T[:X]
  ans += sum(c)
print(ans)
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort()

ans = sum(p[0:X]) + sum(q[0:Y])

for i in range(C):
    if p[-1] <= q[-1] < r[-1] and A > 0:
        ans += r.pop(-1)
        ans -= p.pop(-1)
        A -= 1
    elif q[-1] <= p[-1] < r[-1] and B > 0:
        ans += r.pop(-1)
        ans -= q.pop(-1)
        B -= 1
    elif q[-1] < r[-1] and B > 0:
        ans += r.pop(-1)
        ans -= q.pop(-1)
        B -= 1
    elif p[-1] < r[-1] and A > 0:
        ans += r.pop(-1)
        ans -= p.pop(-1)
        A -= 1
    else:
        break
print(ans)

Failing test case input:
0 2 8 -2 8
8 5 3 1
4 34 000 44
2 1 4 1

Expected output:
78


Actual output:
81


Fixed submission (verdict: Accepted):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort()

ans = sum(p[0:X]) + sum(q[0:Y])
p = sorted(p[0:X], reverse=True)
q = sorted(q[0:Y], reverse=True)

for i in range(C):
    if p and q and r:
        if p[-1] <= q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        elif q[-1] <= p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        elif p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif p and r:
        if p[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= p.pop(-1)
        else:
            break
    elif q and r:
        if q[-1] < r[-1]:
            ans += r.pop(-1)
            ans -= q.pop(-1)
        else:
            break
    else:
        break

print(ans)
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x, y, a, b, c = map(int, input().split())
arrayP = list(input().split())
arrayQ = list(input().split())
arrayR = list(input().split())
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(len(arrayR)):
    if arrayAns[0] <= int(arrayR[i]):
        arrayAns[0] = int(arrayR[i])
        arrayAns.sort()
print(sum(arrayAns))

Failing test case input:
0 2 4 0 8
5 12 2 1
4 21 68 4
2 2 2 0

Expected output:
89


Actual output:
72


Fixed submission (verdict: Accepted):
x, y, a, b, c = map(int, input().split())
arrayP = list(map(int, input().split()))
arrayQ = list(map(int, input().split()))
arrayR = list(map(int, input().split()))
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(min(len(arrayR), len(arrayAns))):
    if arrayAns[i] <= int(arrayR[i]):
        arrayAns[i] = int(arrayR[i])
    else:
        break
print(sum(arrayAns))

Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x,y,a,b,c= list(map(int,input().split()))

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort()

t=p[0:x] + q[0:y] + r
t.sort()
print(sum(t[0:(x+y)]))

Failing test case input:
1 2 2 4 1
3 3
1 1
3

Expected output:
7


Actual output:
5


Fixed submission (verdict: Accepted):
X,Y,A,B,C = list(map(int,input().split()))
p = sorted(list(map(int,input().split())),reverse=True)
q = sorted(list(map(int,input().split())),reverse=True)
r = sorted(list(map(int,input().split())),reverse=True)

taberu = p[0:X] + q[0:Y] + r
taberu.sort(reverse=True)

print(sum(taberu[0:X+Y]))
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  insort(p_list,  int(value))

for value in input().split():
  insort(q_list,  int(value))
  
for value in input().split():
  insort(r_list,  int(value))
  
red = 0
green = 0
white = 0

sum_num = 0

print(sum_num)
for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

Failing test case input:
2 2 1 4 4
11 12 13 14
21 22 8 24
1 2 3 4

Expected output:
73


Actual output:
0


Fixed submission (verdict: Accepted):
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
r_list = list(map(int,input().split()))


p_list.sort()
q_list.sort()
r_list.sort()

red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)
Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x,y,a,b,c = map(int,input().split())
p_ = list(map(int,input().split()))
q_ = list(map(int,input().split()))
r_ = list(map(int,input().split()))
p_.sort(reverse=True)
q_.sort(reverse=True)
r_.sort(reverse=True)
red = x-1
gre = y-1
mu = 0
while True:
    if red < 0 or gre < 0 or mu == c:
        break
    else:
        badr = p_[red]
        badg = q_[gre]
        badmu = r_[mu]
        if badr <= badg:
            if badr < badmu:
                red = red-1
                mu = mu+1
            else:
                break
        else:
            if badg < badmu:
                gre = gre-1
                mu = mu+1
            else:
                break
ans = 0
for i in range(red+1):
    ans += p_[i]
for i in range(gre+1):
    ans += q_[i]
for i in range(mu):
    ans += r_[i]
print(ans)

Failing test case input:
0 2 2 2 3
8 1
9 1
2 1

Expected output:
11


Actual output:
10


Fixed submission (verdict: Accepted):
x,y,a,b,c = map(int,input().split())
p_ = list(map(int,input().split()))
q_ = list(map(int,input().split()))
r_ = list(map(int,input().split()))
p_.sort(reverse=True)
q_.sort(reverse=True)
r_.sort(reverse=True)
red = x-1
gre = y-1
mu = 0
while True:
    if mu == c:
        break
    else:
        if red >= 0:
            badr = p_[red]
        else:
            badr = 1000000000
        if gre >= 0:
            badg = q_[gre]
        else:
            badg = 1000000000
        badmu = r_[mu]
        if badr <= badg:
            if badr < badmu:
                red = red-1
                mu = mu+1
            else:
                break
        else:
            if badg < badmu:
                gre = gre-1
                mu = mu+1
            else:
                break
ans = 0
for i in range(red+1):
    ans += p_[i]
for i in range(gre+1):
    ans += q_[i]
for i in range(mu):
    ans += r_[i]
print(ans)

Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
X, Y, A, B, C = (int(x) for x in input().split())
R = [int(i) for i in input().split()]
G = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

print(R, G, M)

R.sort(reverse=True)
G.sort(reverse=True)

for i in range(X):
  M.append(R[i])

for i in range(Y):
  M.append(G[i])

M.sort(reverse=True)

ans = 0
for i in range(X + Y):
  ans = M[i] + ans

print(ans)


Failing test case input:
0 4 4 -1 4
15 22 10 1
18 15 31 33
1 1 2 1

Expected output:
97


Actual output:
[15, 22, 10, 1] [18, 15, 31, 33] [1, 1, 2, 1]
97


Fixed submission (verdict: Accepted):
X, Y, A, B, C = (int(x) for x in input().split())
R = [int(i) for i in input().split()]
G = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

R.sort(reverse=True)
G.sort(reverse=True)

for i in range(X):
  M.append(R[i])

for i in range(Y):
  M.append(G[i])

M.sort(reverse=True)

ans = 0
for i in range(X + Y):
  ans = M[i] + ans

print(ans)
Problem: p02727
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

sa = sum(p[:X])
sb = sum(q[:Y])

p.append(float('inf'))
q.append(float('inf'))


ans = sa + sb
nr = 0
np = X-1
nq = Y-1
while nr < len(r):
  if r[nr] <= min(p[np], q[nq]):
    break
  elif p[np] > r[nr] > q[nq]:
    ans += r[nr] - q[nq]
    nr += 1
    nq -= 1
    continue
  else:
    ans += r[nr] - p[np]
    nr += 1
    nq -= 1
    continue
  
print(ans)

Failing test case input:
2 0 -2 0 6
28 -2
9 -1
2 -1

Expected output:
30


Actual output:
31


Fixed submission (verdict: Accepted):
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

sa = sum(p[:X])
sb = sum(q[:Y])

p.append(float('inf'))
q.append(float('inf'))


ans = sa + sb
nr = 0
np = X-1
nq = Y-1
while nr < len(r):
  if r[nr] <= min(p[np], q[nq]):
    break
  elif p[np] > q[nq]:
    ans += r[nr] - q[nq]
    nr += 1
    nq -= 1
    continue
  else:
    ans += r[nr] - p[np]
    nr += 1
    np -= 1
    continue

  
print(ans)

Problem: p02727
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
p = sorted(p)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r=r+p
r=r+q
r = sorted(r)
print(sum(r[(len(r)-x-y):]))


Failing test case input:
0 2 5 0 8
5 12 2 1
4 23 95 5
4 0 4 1

Expected output:
118


Actual output:
100


Fixed submission (verdict: Accepted):
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
q = sorted(q)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r = r + p
r = r + q
r = sorted(r)
print(sum(r[(len(r)-x-y):]))
Problem: p02727
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from heapq import heappush, heappop

x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

hp = []
for e in p:
    heappush(hp, [-e, 0])
for e in q:
    heappush(hp, [-e, 1])
for e in r:
    heappush(hp, [-e, 2])

ans = 0
cnt = [0] * 3
sm = 0
mx = [c, x, y]
while hp:
    yum, col = heappop(hp)
    yum = -yum

    if cnt[col] >= mx[col]:
        continue

    ans += yum
    cnt[col] += 1
    sm += 1

    if sm == x + y:
        break

print(ans)


Failing test case input:
0 2 4 0 8
5 13 2 1
4 38 55 44
2 2 2 1

Expected output:
99


Actual output:
18


Fixed submission (verdict: Accepted):
from heapq import heappush, heappop

x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

hp = []
for e in p:
    heappush(hp, [-e, 0])
for e in q:
    heappush(hp, [-e, 1])
for e in r:
    heappush(hp, [-e, 2])

ans = 0
cnt = [0] * 3
sm = 0
mx = [x, y, c]
while hp:
    yum, col = heappop(hp)
    yum = -yum

    if cnt[col] >= mx[col]:
        continue

    ans += yum
    cnt[col] += 1
    sm += 1

    if sm == x + y:
        break

print(ans)
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import math
from bisect import bisect_left, bisect_right

N = int(input())
X = list(map(int, input().split()))
L = int(input())
Q = int(input())
Y = [list(map(int, input().split())) for _ in range(Q)]

LOGN = int(math.log(N) / math.log(2)) + 1
parent = [[-1] * N for _ in range(LOGN + 1)]


for k in range(LOGN + 1):
    for i in range(N):
        if k == 0:
            parent[k][i] = bisect_right(X, X[i] + L) - 1
        else:
            parent[k][i] = parent[k - 1][parent[k - 1][i]]

for a, b in Y:
    a -= 1
    b -= 1
    if a > b:
        a, b = b, a
    i = bisect_left([parent[j][a] for j in range(LOGN + 1)], b)
    print(2 ** i)


Failing test case input:
9
1 3 6 13 23 18 19 29 31
12
2
1 8
2 5
9 7
6 7

Expected output:
3
2


Actual output:
4
2


Fixed submission (verdict: Accepted):
import math
from bisect import bisect_left, bisect_right

N = int(input())
X = list(map(int, input().split()))
L = int(input())
Q = int(input())
Y = [list(map(int, input().split())) for _ in range(Q)]

LOGN = int(math.log(N) / math.log(2)) + 1
parent = [[-1] * N for _ in range(LOGN + 1)]


for k in range(LOGN + 1):
    for i in range(N):
        if k == 0:
            parent[k][i] = bisect_right(X, X[i] + L) - 1
        else:
            parent[k][i] = parent[k - 1][parent[k - 1][i]]

def find(a, b):
    num = 0
    u = a
    while True:
        k = 0
        for i in range(LOGN + 1):
            if parent[i][u] >= b:
                k = i
                break
                
        if k == 0:
            num += 1
            break
        num += pow(2, k - 1)
        u = parent[k - 1][u]
        
    return num

for a, b in Y:
    a -= 1
    b -= 1
    if a > b:
        a, b = b, a
    print(find(a, b))
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import bisect
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
d = int(input())
graph = [[0 for i in range(n+1)] for j in range(18)]
for i in range(n):
  x = bisect.bisect_right(a,a[i]+d)
  graph[0][i+1] = x
for j in range(1,18):
  for i in range(n):
    t = graph[j-1][i+1]
    graph[j][i+1] = graph[j-1][t]
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  x,y = min(x,y),max(x,y)
  ans = 0
  for j in range(18)[::-1]:
    if graph[j][x] <= y:
      ans += 2**j
      x = graph[j][x]
    if j == 0 and x < y:
      ans += 1
  print(ans)

Failing test case input:
9
1 3 6 13 23 18 19 29 31
10
2
1 9
7 2
10 7
8 10

Expected output:
5
2


Actual output:
262143
2


Fixed submission (verdict: Accepted):
import bisect
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
d = int(input())
graph = [[0 for i in range(n+1)] for j in range(18)]
for i in range(n):
  x = bisect.bisect_right(a,a[i]+d)
  graph[0][i+1] = x
for j in range(1,18):
  for i in range(n):
    t = graph[j-1][i+1]
    graph[j][i+1] = graph[j-1][t]
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  x,y = min(x,y),max(x,y)
  ans = 0
  for j in range(18)[::-1]:
    if graph[j][x] < y:
      ans += 2**j
      x = graph[j][x]
    if j == 0 and x < y:
      ans += 1
  print(ans)
Problem: p04017
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    for _ in range(int.bit_length(N - 1) + 1):
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while a < b - 1:
            u = bisect(Unreachs[a], b) - 1
            ans += 2 ** u
            a = Unreachs[a][u]
        ans += 1
        print(ans)
    return 0

main()

Failing test case input:
9
1 3 6 13 23 18 19 29 31
10
2
1 8
7 3
9 7
8 5

Expected output:
4
2


Actual output:
3.5
2.5


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    updated = True
    while updated:
        updated = False
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            updated = updated or (u != Unreachs[i][-1])
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while True:
            k = bisect(Unreachs[a], b) - 1
            if k < 0:
                ans += 1
                break
            ans += 2 ** k
            a = Unreachs[a][k] - 1
        print(ans)
    return 0

main()
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

from bisect import bisect

n = int(input())
x = list(map(int, input().split()))
l = int(input())

m = 20
par = [[-1]*n for i in range(m)]

for i in range(n):
    j = bisect(x, x[i]+l)-1
    par[0][i] = j

for i in range(m-1):
    for j in range(n):
        par[i+1][j]=par[i][par[i][j]]

def hoge(x, y):
    k = 0
    for i in range(m)[::-1]:
        if par[i][x] < y:
            k += 1<<i
            x = par[i][x]
        elif i == 0 and par[i][x] >= y:
            k += 1<<i
    return k

q = int(input())
ans = []
for i in range(q):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    if a > b:
        a,b = b,a
    print(hoge(a,b))

Failing test case input:
9
1 3 6 13 23 19 19 29 31
10
4
1 9
7 6
6 7
8 1

Expected output:
5
1
1
4


Actual output:
5
1
1
3


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

from bisect import bisect

n = int(input())
x = list(map(int, input().split()))
l = int(input())

m = 20
par = [[-1]*n for i in range(m)]

for i in range(n):
    j = bisect(x, x[i]+l)-1
    par[0][i] = j

for i in range(m-1):
    for j in range(n):
        par[i+1][j]=par[i][par[i][j]]

def hoge(x, y):
    k = 0
    for i in range(m)[::-1]:
        if par[i][x] < y:
            k += 1<<i
            x = par[i][x]
    return k+1

q = int(input())
ans = []
for i in range(q):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    if a > b:
        a,b = b,a
    print(hoge(a,b))
Problem: p04017
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    for _ in range(int.bit_length(N - 1) + 1):
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while True:
            u = bisect(Unreachs[a], b) - 1
            if u == -1:
                break
            ans += 2 ** u
            a = Unreachs[a][u]
        ans += 1
        print(ans)
    return 0

main()

Failing test case input:
9
1 7 6 13 23 18 19 29 31
10
3
1 8
7 3
6 1
8 10

Expected output:
4
2
3


Actual output:
3
2
3


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    updated = True
    while updated:
        updated = False
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            updated = updated or (u != Unreachs[i][-1])
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while True:
            k = bisect(Unreachs[a], b) - 1
            if k < 0:
                ans += 1
                break
            ans += 2 ** k
            a = Unreachs[a][k] - 1
        print(ans)
    return 0

main()
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

from bisect import bisect

n = int(input())
x = list(map(int, input().split()))
l = int(input())

m = 20
par = [[-1]*n for i in range(m)]

for i in range(n-1):
    j = bisect(x, x[i]+l)-1
    par[0][i] = j

for i in range(m-1):
    for j in range(n):
        par[i+1][j]=par[i][par[i][j]]

def hoge(x, y):
    k = 0
    for i in range(m)[::-1]:
        if 0 <= par[i][x] <= y:
            k += 1<<i
            x = par[i][x]
    return k

q = int(input())
ans = []
for i in range(q):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    if a > b:
        a,b = b,a
    ans.append(hoge(a, b))

print(*ans, sep="\n")

Failing test case input:
9
1 3 6 13 23 18 19 29 31
12
2
1 8
1 5
9 7
6 7

Expected output:
3
2


Actual output:
2
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

from bisect import bisect

n = int(input())
x = list(map(int, input().split()))
l = int(input())

m = 20
par = [[-1]*n for i in range(m)]

for i in range(n):
    j = bisect(x, x[i]+l)-1
    par[0][i] = j

for i in range(m-1):
    for j in range(n):
        par[i+1][j]=par[i][par[i][j]]

def hoge(x, y):
    k = 0
    for i in range(m)[::-1]:
        if par[i][x] < y:
            k += 1<<i
            x = par[i][x]
    return k+1

q = int(input())
ans = []
for i in range(q):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    if a > b:
        a,b = b,a
    print(hoge(a,b))
Problem: p04017
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


N = I()
x = LI()
L = I()

from bisect import bisect_right

arrive = [[0]*N for _ in range(30)]  
for k in range(30):
    if k == 0:
        for i in range(N):
            arrive[0][i] = bisect_right(x,x[i]+L)-1
    else:
        for i in range(N):
            arrive[k][i] = arrive[k-1][arrive[k-1][i]]
print(arrive)


def query(a,b):  
    a -= 1
    b -= 1
    ans = 0
    for k in range(29,-1,-1):
        if arrive[k][a] > b:
            continue
        else:
            a = arrive[k][a]
            ans += 1 << k
    print(ans)


Q = I()
for i in range(Q):
    a,b = MI()
    if a > b:
        a,b = b,a
    query(a,b)


Failing test case input:
9
1 3 6 13 23 18 19 29 31
10
2
1 8
7 3
9 10
8 7

Expected output:
4
2


Actual output:
[[2, 3, 3, 6, 8, 6, 7, 8, 8], [3, 6, 6, 7, 8, 7, 8, 8, 8], [7, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8]]
4
2


Fixed submission (verdict: Accepted):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


N = I()
x = LI()
L = I()

from bisect import bisect_right

arrive = [[0]*N for _ in range(30)]  
for k in range(30):
    if k == 0:
        for i in range(N):
            arrive[0][i] = bisect_right(x,x[i]+L)-1
    else:
        for i in range(N):
            arrive[k][i] = arrive[k-1][arrive[k-1][i]]


def query(a,b):  
    a -= 1
    b -= 1
    ans = 1
    for k in range(29,-1,-1):
        if arrive[k][a] >= b:
            continue
        else:
            a = arrive[k][a]
            ans += 1 << k
    print(ans)


Q = I()
for i in range(Q):
    a,b = MI()
    if a > b:
        a,b = b,a
    query(a,b)
Problem: p04017
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    updated = True
    while updated:
        updated = False
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            updated = updated or (u != Unreachs[i][-1])
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while True:
            k = bisect(Unreachs[a], b) - 1
            if k < 0:
                ans += 1
                break
            ans += 2 ** k
            a = Unreachs[a][k]
        print(ans)
    return 0

main()

Failing test case input:
9
1 7 6 13 23 18 19 29 31
10
3
1 8
7 3
6 1
8 10

Expected output:
4
2
3


Actual output:
3
2
3


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
from functools import partial, reduce
from operator import mul
prod = partial(reduce, mul)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from bisect import bisect

def main():
    N = II()
    X = LI()  
    X.append(INF)  
    L = II()  
    Q = II()
    AB = []  
    for _ in range(Q):
        AB.append(LI_())
    Unreachs = [[] for _ in range(N)]
    
    for i, x in enumerate(X[:-1]):
        u = bisect(X, x + L)
        Unreachs[i].append(u)
    
    updated = True
    while updated:
        updated = False
        for i in range(N):
            u = Unreachs[Unreachs[i][-1] - 1][-1]
            updated = updated or (u != Unreachs[i][-1])
            Unreachs[i].append(u)
    
    for a, b in AB:
        if a > b:
            a, b = b, a
        ans = 0
        while True:
            k = bisect(Unreachs[a], b) - 1
            if k < 0:
                ans += 1
                break
            ans += 2 ** k
            a = Unreachs[a][k] - 1
        print(ans)
    return 0

main()
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from bisect import bisect_right
N = int(input())
x = list(map(int, input().split()))
L = int(input())
n = N.bit_length()
next_hotel = [[0]*n for _ in range(N)]
for i in range(N):
    index = bisect_right(x, x[i]+L)-1
    
    next_hotel[i][0] = index
for i in range(1, n):
    for j in range(N):
        next_hotel[j][i] = next_hotel[next_hotel[j][i-1]][i-1]
def count(a, b):
    if a>b:
        a, b = b, a
    res = 0
    for i in range(n):
        if a == b:
            return  res
        c = bisect_right(next_hotel[a], b)-1
        a = next_hotel[a][c]
        res+=2**c
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    print(count(a-1, b-1))

Failing test case input:
9
1 3 10 13 15 18 19 29 40
10
3
2 6
7 2
6 3
8 5

Expected output:
2
2
1


Actual output:
None
2
None


Fixed submission (verdict: Accepted):
from bisect import bisect_left, bisect_right
N = int(input())
x = list(map(int, input().split()))
L = int(input())
n = N.bit_length()+1
next_hotel = [[0]*n for _ in range(N)]
for i in range(N):
    index = bisect_right(x, x[i]+L)-1
    if index == i:
        next_hotel[i][0] = N
    else:
        next_hotel[i][0] = index
for i in range(1, n):
    for j in range(N):
        if next_hotel[j][i-1]<N:
            index = next_hotel[next_hotel[j][i-1]][i-1]
            if index == j:
                next_hotel[j][i] = N
            else:
                next_hotel[j][i] = index
        else:
            next_hotel[j][i] = N
def count(a, b):
    if a>b:
        a, b = b, a
    res = 0
    for i in range(n):
        if a >= b:
            return  res
        c = max(0, bisect_left(next_hotel[a], b)-1)
        a = next_hotel[a][c]
        res+=2**c
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    print(count(a-1, b-1))
Problem: p04017
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from bisect import bisect_right
N = int(input())
X = list(map(int, input().split()))
L = int(input())

P = [[0] * N for i in range(30)]
for i in range(N):
    q = bisect_right(X, X[i] + L)
    P[0][i] = q - 1

for i in range(1, 30):
    for j in range(N):
        P[i][j] = P[i - 1][P[i - 1][j]]

Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    if a > b:
        a, b = b, a
    Y = X[b]
    num = 0
    for j in range(29, -1, -1):
        if X[P[j][a]] <= Y:
            Y -= X[P[j][a]]
            a = P[j][a]
            num += 2 ** j
        if Y <= 0:
            print(num)
            break


Failing test case input:
9
1 3 6 13 23 18 19 29 31
10
2
1 9
7 2
10 7
8 10

Expected output:
5
2


Actual output:
536870912
2


Fixed submission (verdict: Accepted):
from bisect import bisect_right
N = int(input())
X = list(map(int, input().split()))
L = int(input())

P = [[0] * N for i in range(30)]
for i in range(N):
    q = bisect_right(X, X[i] + L)
    P[0][i] = q - 1

for i in range(1, 30):
    for j in range(N):
        P[i][j] = P[i - 1][P[i - 1][j]]


Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    a, b = a - 1, b - 1
    num = 0
    for j in range(29, -1, -1):
        if P[j][a] < b:
            a = P[j][a]
            num += 2 ** j
    print(num + 1)
Problem: p04017
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from bisect import bisect_right
N = int(input())
x = list(map(int, input().split()))
L = int(input())
n = N.bit_length()
next_hotel = [[0]*n for _ in range(N)]
for i in range(N):
    index = bisect_right(x, x[i]+L)-1
    
    next_hotel[i][0] = index
for i in range(1, n):
    for j in range(N):
        next_hotel[j][i] = next_hotel[next_hotel[j][i-1]][i-1]
def count(a, b):
    if a>b:
        a, b = b, a
    res = 0
    for i in range(n):
        if a >= b:
            return  res
        c = max(bisect_right(next_hotel[a], b)-1, 0)
        a = next_hotel[a][c]
        res+=2**c
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    print(count(a-1, b-1))

Failing test case input:
9
1 3 6 13 23 19 19 29 31
10
1
1 9
7 3
6 7
8 5

Expected output:
5


Actual output:
8


Fixed submission (verdict: Accepted):
from bisect import bisect_left, bisect_right
N = int(input())
x = list(map(int, input().split()))
L = int(input())
n = N.bit_length()+1
next_hotel = [[0]*n for _ in range(N)]
for i in range(N):
    index = bisect_right(x, x[i]+L)-1
    if index == i:
        next_hotel[i][0] = N
    else:
        next_hotel[i][0] = index
for i in range(1, n):
    for j in range(N):
        if next_hotel[j][i-1]<N:
            index = next_hotel[next_hotel[j][i-1]][i-1]
            if index == j:
                next_hotel[j][i] = N
            else:
                next_hotel[j][i] = index
        else:
            next_hotel[j][i] = N
def count(a, b):
    if a>b:
        a, b = b, a
    res = 0
    for i in range(n):
        if a >= b:
            return  res
        c = max(0, bisect_left(next_hotel[a], b)-1)
        a = next_hotel[a][c]
        res+=2**c
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    print(count(a-1, b-1))
Problem: p04017
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from bisect import bisect_right
N = int(input())
X = list(map(int, input().split()))
L = int(input())

P = [[0] * N for i in range(30)]
for i in range(N):
    q = bisect_right(X, X[i] + L)
    P[0][i] = q - 1

for i in range(1, 30):
    for j in range(N):
        P[i][j] = P[i - 1][P[i - 1][j]]


Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    if a > b:
        a, b = b, a
    Y = X[b]
    num = 0
    for j in range(29, -1, -1):
        if X[P[j][a]] <= Y:
            Y -= X[P[j][a]]
            num += 2 ** j
        if Y <= 0:
            print(num)
            break

Failing test case input:
9
1 8 6 13 23 18 19 22 31
10
2
1 8
7 3
6 7
5 10

Expected output:
3
2


Actual output:

Fixed submission (verdict: Accepted):
from bisect import bisect_right
N = int(input())
X = list(map(int, input().split()))
L = int(input())

P = [[0] * N for i in range(30)]
for i in range(N):
    q = bisect_right(X, X[i] + L)
    P[0][i] = q - 1

for i in range(1, 30):
    for j in range(N):
        P[i][j] = P[i - 1][P[i - 1][j]]


Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    a, b = a - 1, b - 1
    num = 0
    for j in range(29, -1, -1):
        if P[j][a] < b:
            a = P[j][a]
            num += 2 ** j
    print(num + 1)
Problem: p04017
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


N = I()
x = LI()
L = I()

from bisect import bisect_right

arrive = [[0]*N for _ in range(30)]  
for k in range(30):
    if k == 0:
        for i in range(N):
            arrive[0][i] = bisect_right(x,x[i]+L)-1
    else:
        for i in range(N):
            arrive[k][i] = arrive[k-1][arrive[k-1][i]]


def query(a,b):  
    a -= 1
    b -= 1
    ans = 0
    for k in range(29,-1,-1):
        if arrive[k][a] > b:
            continue
        else:
            a = arrive[k][a]
            ans += 1 << k
    print(ans)


Q = I()
for i in range(Q):
    a,b = MI()
    if a > b:
        a,b = b,a
    query(a,b)


Failing test case input:
9
1 6 6 13 23 18 27 29 31
10
2
1 8
7 3
6 7
8 10

Expected output:
5
3


Actual output:
4
3


Fixed submission (verdict: Accepted):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


N = I()
x = LI()
L = I()

from bisect import bisect_right

arrive = [[0]*N for _ in range(30)]  
for k in range(30):
    if k == 0:
        for i in range(N):
            arrive[0][i] = bisect_right(x,x[i]+L)-1
    else:
        for i in range(N):
            arrive[k][i] = arrive[k-1][arrive[k-1][i]]


def query(a,b):  
    a -= 1
    b -= 1
    ans = 1
    for k in range(29,-1,-1):
        if arrive[k][a] >= b:
            continue
        else:
            a = arrive[k][a]
            ans += 1 << k
    print(ans)


Q = I()
for i in range(Q):
    a,b = MI()
    if a > b:
        a,b = b,a
    query(a,b)