Buggy python submission (verdict: Runtime Error):
A, B, Q = [int(a) for a in input().split(" ")]
S = sorted([int(input()) for i in range(A)])
T = sorted([int(input()) for i in range(B)])
X = [int(input()) for i in range(Q)]
INF = float("inf")

def search(arr, x):
    l, r = (0, len(arr))
    while r-l>1:
        med = (l+r)//2
        if arr[med] == x:
            return med
        l, r = (l, med) if x<arr[med] else (med+1, r)
    return l

for x in X:
    s1 = min(search(S, x), len(S)-1)
    t1 = min(search(T, x), len(T)-1)
    ss, ts = None, None
    
    if S[s1]==x:
        ss = (0, 0)
    elif s1==0 and x<S[s1]:
        ss = (INF, abs(S[s1]-x))
    elif s1==len(S)-1 and S[s1]<x:
        ss = (abs(S[s1]-x), INF)
    elif S[s1]<x:
        ss = (abs(S[s1]-x), abs(S[s1+1]-x))
    else:
        ss = (abs(S[s1-1]-x), abs(S[s1]-x))
    
    if T[t1]==x:
        ts = (0, 0)
    elif t1==0 and x<T[t1]:
        ts = (INF, abs(T[t1]-x))
    elif t1==len(T)-1 and T[t1]<x:
        ts = (abs(T[t1]-x), INF)
    elif T[t1]<x:
        ts = (abs(T[t1]-x), abs(T[t1+1]-x))
    else:
        ts = (abs(T[t1-1]-x), abs(T[t1]-x))

    ans = min([max(ss[0], ts[0]), max(ss[1], ts[1]),
               2*ss[0]+ts[1], 2*ss[1]+ts[0], ss[0]+2*ts[1], ss[1]+2*ts[0]])
    print(ans)


Fixed submission:
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))

Buggy python submission (verdict: Time Limit Exceeded):
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans

def dfs(ls):
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    return ans

print(dfs(ls))


Fixed submission:
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))

Buggy python submission (verdict: Time Limit Exceeded):
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace

def solve(ls):
    n = len(ls)
    ans = float("inf")
    qs = [[] for i in range(3)]
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if len(q)<3:
                heappush(q, (val, i))
            elif q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans

def dfs(ls):
    n = len(ls)
    if n < 3:
        return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = ls[i]+ls[j]
            ans = min(ans, 10+dfs(ls2))
    return ans

print(dfs(ls))


Fixed submission:
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))

Buggy python submission (verdict: Time Limit Exceeded):
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]

def solve(ls):
    n = len(ls)
    ans = float("inf")
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if i!=j and j!=k and i!=k:
                    tmp = abs(ls[i]-A)+abs(ls[j]-B)+abs(ls[k]-C)
                    ans = min(ans, tmp)
    return ans

def dfs(ls):
    n = len(ls)
    if n < 3:
        return float("inf")
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = ls[i]+ls[j]
            ans = min(ans, 10+dfs(ls2))
    return ans

print(dfs(ls))

Fixed submission:
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))