Problem p03112 (python)

Fixed submission:
#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
"""
n, a, b, c = map(int, input().split())
abc = [a,b,c]
x = [int(input()) for i in range(n)]
x.sort()
ans = []
flg = 0
for i in x:
    if i in abc:
        ans.append(i)
        flg = 1
if flg:
    for i in ans:
        abc.remove(i)
        x.remove(i)

"""
#D

a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)

Buggy submission (verdict: Runtime Error):
def main():
  inf = float("INF")
  a,b,q = map(int, input().split())
  s = [None] * (a + 3)
  s[0:2] = [-inf,-inf]
  s[-1] = inf
  t = [None] * (b + 3)
  t[0:2] = [-inf,-inf]
  t[-1] = inf
  for i in range(a):
      s[i+2] = int(input())
  for i in range(b):
      t[i+2] = int(input())

  from bisect import bisect_left

  dp0 = [None] * (a+3)
  dp1 = [None] * (b+3)

  for _ in range(q):
    ans = inf
    x = int(input())
    sr = bisect_right(s, x)
    tr = bisect_right(t, x)    
    for si in (sr, sr-1):
      for ti in (tr, tr-1):
        ans = min(ans, abs(x-s[si]) + abs(s[si]-t[ti]), abs(x-t[ti]) + abs(s[si]-t[ti]))
    print(ans)
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
inf = float("INF")
a,b,q = map(int, input().split())
s = [-inf, -inf]+[int(input()) for _ in range(a)]+[inf]
t = [-inf, -inf]+[int(input()) for _ in range(b)]+[inf]

from bisect import bisect_left
from collections import defauldict

dp = defaultdict(lambda: None)

def f(s,t,x,ss,i):
  if dp[(ss,i)] != None: return abs(x - s[ss]) + dp[(ss,i)]
  tr = bisect_left(t, s[ss])
  tl = tr - 1
  dp[(ss,i)] = min(abs(s[ss] - t[tl]), abs(s[ss] - t[tr]))
  return abs(x - s[ss]) + dp[(ss,i)]


for _ in range(q):
  ans = inf
  x = int(input())
  sr = bisect_left(s, x)
  ans = min(ans, f(s,t,x,sr,0))
  ans = min(ans, f(s,t,x,sr - 1,0))
  tr = bisect_left(t, x)
  ans = min(ans, f(t,s,x,tr,1))
  ans = min(ans, f(t,s,x,tr - 1,1))
  print(ans)

Buggy submission (verdict: Runtime Error):
#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
import itertools

n, a, b, c = map(int, input().split())
abc = [a, b, c]
ans = float("INF")
x = [int(input()) for i in range(n)]
nums = list(itertools.product([0, 1, 2, 3], repeat = n))
#print(nums)
for i in nums:
    tmp = 0
    aa = 0
    ba = 0
    ca = 0
    for number, k in enumerate(i):
        if k == 1:
            if aa != 0:
                tmp += 10
            aa += x[number]
        if k == 2:
            if ba != 0:
                tmp += 10
            ba += x[number]
        if k == 3:
            if ca != 0:
                tmp += 10
            ca += x[number]
    if aa == 0 or ba == 0 or ca == 0:
        tmp = float("INF")
    else:
        tmp += abs(aa - a)
        tmp += abs(ba - b)
        tmp += abs(ca - c)
    ans = min(tmp, ans)
print(ans)
        
        
            


#D

"""
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)
"""