qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import collections
from copy import deepcopy

n,a,b,c=map(int,input().split())
arr=[int(input()) for _ in range(n)]
l=collections.deque()
l.append([[],[],[]])
cnt=0

while 1:
  if cnt==8:
    break
  base=l.popleft()
  if len(base[0])==0 and len(base[1])==0 and len(base[2])==0:
    l.append([[arr[cnt]],[],[]])
    l.append([[],[arr[cnt]],[]])
    l.append([[],[],[arr[cnt]]])
    l.append([[],[],[]])
    cnt+=1
  else:
    tmp1=deepcopy(base)
    tmp1[0].append(arr[cnt])
    l.append(tmp1)
    tmp2=deepcopy(base)
    tmp2[1].append(arr[cnt])
    l.append(tmp2)
    tmp3=deepcopy(base)
    tmp3[2].append(arr[cnt])
    l.append(tmp3)
    tmp4=deepcopy(base)
    l.append(tmp4)

mincost=10**9
for bl in l:
  if len(bl[0])==0 or len(bl[1])==0 or len(bl[2])==0:
    continue
  else:
    tmpcost=abs(sum(bl[0])-a)+abs(sum(bl[1])-b)+abs(sum(bl[2])-c)+10*(len(bl[0])-1+len(bl[1])-1+len(bl[2])-1)
    if mincost>tmpcost:
      mincost=tmpcost
print(mincost)

Fixed code:
import collections
from copy import deepcopy

n,a,b,c=map(int,input().split())
arr=[int(input()) for _ in range(n)]
l=collections.deque()
l.append([[],[],[]])
cnt=0

while 1:
  if cnt==n:
    break
  base=l.popleft()
  if len(base[0])==0 and len(base[1])==0 and len(base[2])==0:
    l.append([[arr[cnt]],[],[]])
    l.append([[],[arr[cnt]],[]])
    l.append([[],[],[arr[cnt]]])
    l.append([[],[],[]])
    cnt+=1
  else:
    tmp1=deepcopy(base)
    tmp1[0].append(arr[cnt])
    l.append(tmp1)
    tmp2=deepcopy(base)
    tmp2[1].append(arr[cnt])
    l.append(tmp2)
    tmp3=deepcopy(base)
    tmp3[2].append(arr[cnt])
    l.append(tmp3)
    tmp4=deepcopy(base)
    l.append(tmp4)

mincost=10**9
for bl in l:
  if len(bl[0])==0 or len(bl[1])==0 or len(bl[2])==0:
    continue
  else:
    tmpcost=abs(sum(bl[0])-a)+abs(sum(bl[1])-b)+abs(sum(bl[2])-c)+10*(len(bl[0])-1+len(bl[1])-1+len(bl[2])-1)
    if mincost>tmpcost:
      mincost=tmpcost
print(mincost)
Buggy code:
import itertools
INF = 1000000007
n, a, b, c = map(int, input().split())
B = [a, b, c]
take = [int(input()) for i in range(n)]
sh = [i for i in itertools.product(range(4), repeat=n)]
res = INF
for C in sh:
    ex = [0] * 3
    ifres = 0
    for index, x in enumerate(C):
        if x != 3:
            if ex[x] != 0:
                ifres += 10
            ex[x] += take[index]
    for i in range(3):
        ifres += abs(B[i]-ex[i])
    if ifres < res:
        res = ifres
print(res)

Fixed code:
import itertools
INF = 1000000007
n, a, b, c = map(int, input().split())
B = [a, b, c]
take = [int(input()) for i in range(n)]
sh = [i for i in itertools.product(range(4), repeat=n)]
res = INF
for C in sh:
    if 0 not in C or 1 not in C or 2 not in C:
        continue
    ex = [0] * 3
    ifres = 0
    for index, x in enumerate(C):
        if x != 3:
            if ex[x] != 0:
                ifres += 10
            ex[x] += take[index]
    for i in range(3):
        ifres += abs(B[i]-ex[i])
    if ifres < res:
        res = ifres
print(res)
Buggy code:
n,a,b,c = map(int, input().split())
l = [int(input()) for _ in range(n)]
def dfs(k,p,q,r):
  if k == n:
    if min(a,b,c) > 0:
      return abs(p-a)+abs(q-b)+abs(r-c)-30
    else:
      return 10**5
  t1 = dfs(k+1,p,q,r)
  t2 = dfs(k+1,p+l[k],q,r)+10
  t3 = dfs(k+1,p,q+l[k],r)+10
  t2 = dfs(k+1,p,q,r+l[k])+10
  return min(t1,t2,t3,t4)

print(dfs(0,0,0,0))

Fixed code:
n,a,b,c = map(int, input().split())
l = [int(input()) for _ in range(n)]
def dfs(k,p,q,r):
  if k == n:
    if min(p,q,r) > 0:
      return abs(p-a)+abs(q-b)+abs(r-c)-30
    else:
      return 10**7
  t1 = dfs(k+1,p,q,r)
  t2 = dfs(k+1,p+l[k],q,r)+10
  t3 = dfs(k+1,p,q+l[k],r)+10
  t4 = dfs(k+1,p,q,r+l[k])+10
  return min(t1,t2,t3,t4)

print(dfs(0,0,0,0))
Buggy code:
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
INF = 10**9

def dfs(cur,a,b,c):
    if(cur == N):
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c)>0 else INF
    ret0 = dfs(cur+1, a, b, c)
    re1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b+l[cur],c)+10
    ret3 = dfs(cur + 1,a,b,c+l[cur])+10
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Fixed code:
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
INF = 10**9

def dfs(cur,a,b,c):
    if(cur == N):
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c)>0 else INF
    ret0 = dfs(cur+1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b+l[cur],c)+10
    ret3 = dfs(cur + 1,a,b,c+l[cur])+10
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))
Buggy code:
n, a, b, c = list(map(int, input().split()))
ls = [int(input()) for _ in range(n)]

min_ans = 10 ** 10
for i in range(4 ** n):
	mm = [[], [], [],[]]
	tmp = i
	for i2 in range(n):
		g = tmp // (4 ** (n - i2))
		tmp = tmp % (4 ** (n - i2))
		mm[g%4].append(ls[i2])
	
	if len(mm[0]) * len(mm[1]) * len(mm[2]) == 0:
		continue

	ans = abs(sum(mm[0]) - a) + abs(sum(mm[1]) - b) + abs(sum(mm[2]) - c) + (len(mm[0]) - 1) * 10 + (len(mm[1]) - 1) * 10 + (len(mm[2]) - 1) * 10
	min_ans = min(ans, min_ans)

print(min_ans)

Fixed code:
n, a, b, c = list(map(int, input().split()))
ls = [int(input()) for _ in range(n)]

min_ans = 10 ** 10
for i in range(4 ** n):
	mm = [[], [], [],[]]
	tmp = i
	for i2 in range(n):
		g = tmp // (4 ** (n - i2 - 1))
		tmp = tmp % (4 ** (n - i2 - 1))
		mm[g%4].append(ls[i2])
	
	if len(mm[0]) * len(mm[1]) * len(mm[2]) == 0:
		continue

	#print(mm)
	ans = abs(sum(mm[0]) - a) + abs(sum(mm[1]) - b) + abs(sum(mm[2]) - c) + (len(mm[0]) - 1) * 10 + (len(mm[1]) - 1) * 10 + (len(mm[2]) - 1) * 10
	min_ans = min(ans, min_ans)

print(min_ans)
Buggy code:
from copy import copy
N, A, B, C = map(int, input().split())
L = []
for _ in range(N):
  L.append(int(input()))

INF = 10 ** 9
def dfs(cur, a, b, c, m):
  if cur == N-1:
    if min(a, b, c) == 0:
      return INF
    return abs(A-a)+abs(B-b)+abs(C-c)+(m-3)*10

  res0 = dfs(cur+1, a, b, c, m)
  res1 = dfs(cur+1, a+L[cur], b, c, m+1)
  res2 = dfs(cur+1, a, b+L[cur], c, m+1)
  res3 = dfs(cur+1, a, b, c+L[cur], m+1)

  return min(res0, res1, res2, res3)

print(dfs(0, 0, 0, 0, 0))


Fixed code:
from copy import copy
N, A, B, C = map(int, input().split())
L = []
for _ in range(N):
  L.append(int(input()))

INF = 10 ** 9
def dfs(cur, a, b, c, m):
  if cur == N:
    if min(a, b, c) == 0:
      return INF
    return abs(A-a)+abs(B-b)+abs(C-c)+(m-3)*10

  res0 = dfs(cur+1, a, b, c, m)
  res1 = dfs(cur+1, a+L[cur], b, c, m+1)
  res2 = dfs(cur+1, a, b+L[cur], c, m+1)
  res3 = dfs(cur+1, a, b, c+L[cur], m+1)

  return min(res0, res1, res2, res3)

print(dfs(0, 0, 0, 0, 0))
Buggy code:
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]

INF = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        if min(a, b, c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return INF
    ret1 = dfs(cur+1, a, b, c)
    ret2 = dfs(cur+1, a+L[cur], b, c) + 10
    ret3 = dfs(cur+1, a, b+L[cur], c) + 10
    ret4 = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(ret1 + ret2 + ret3 + ret4)

print(dfs(0,0,0,0))


Fixed code:
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]

INF = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        if min(a, b, c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return INF
    ret1 = dfs(cur+1, a, b, c)
    ret2 = dfs(cur+1, a+L[cur], b, c) + 10
    ret3 = dfs(cur+1, a, b+L[cur], c) + 10
    ret4 = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(ret1, ret2, ret3, ret4)

print(dfs(0,0,0,0))
Buggy code:
N,a,b,c=map(int,input().split())
L=[int(input()) for i in range(N)]
import itertools 
ans=300000
for i in itertools.product('ABCD',repeat=N):
  cst=0
  A=[]
  B=[]
  C=[]
  for j in range(N):
    if i[j]=='A':
      A.append(L[j])
    elif i[j]=='B':
      B.append(L[j]) 
    elif i[j]=='C':
      C.append(L[j])
  if len(A)>0 and len(B)>0 and len(C)>0:
    if len(A)>1:
      cst+=len(A)-1
    cst+=abs(sum(A)-a)
    if len(B)>1:
      cst+=len(B)-1
    cst+=abs(sum(B)-b)
    if len(C)>1:
      cst+=len(C)-1
    cst+=abs(sum(C)-c)
    ans=min(ans,cst)
 
print(ans)
   

Fixed code:
N,a,b,c=map(int,input().split())
L=[int(input()) for i in range(N)]
import itertools 
ans=300000
for i in itertools.product('ABCD',repeat=N):
  cst=0
  A=[]
  B=[]
  C=[]
  for j in range(N):
    if i[j]=='A':
      A.append(L[j])
    elif i[j]=='B':
      B.append(L[j]) 
    elif i[j]=='C':
      C.append(L[j])
  if len(A)>0 and len(B)>0 and len(C)>0:
    if len(A)>1:
      cst+=(len(A)-1)*10
    cst+=abs(sum(A)-a)
    if len(B)>1:
      cst+=(len(B)-1)*10
    cst+=abs(sum(B)-b)
    if len(C)>1:
      cst+=(len(C)-1)*10
    cst+=abs(sum(C)-c)
    ans=min(ans,cst)
 
print(ans)
Buggy code:

ans = 100000000000000000000000000
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

#rec part
def rec(a,b,c,gousei,now):
    global ans

    ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

    if now == N:
        return 0

    #A
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)
    rec(a,b,c, gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)

Fixed code:

ans = 100000000000000000000000000
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

#rec part
def rec(a,b,c,gousei,now):
    global ans

    if(a != 0 and b != 0 and c != 0):
        ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

        #print(a,b,c,ans) 

    if now == N:
        return 0


    #A
    rec(a,b,c, gousei,now+1)
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)
Buggy code:
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
      	memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)


Fixed code:
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
        memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)
Buggy code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
if cur == N:
return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
ret0 = dfs(cur + 1, a, b, c)
ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Fixed code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
	if cur == N:
		return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
	ret0 = dfs(cur + 1, a, b, c)
	ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
	ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
	ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
	return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))
Buggy code:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=10**10
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=n+1:
                break
            else:
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

Fixed code:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=10**10
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=n+1:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)
Buggy code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9

def dfs(cur, a, b, c):
	if cur == N:
		return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
	ret0 = dfs(cur + 1, a, b, c)
 	ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
	ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
	ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
	return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))

Fixed code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF=10**9


def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))
Buggy code:
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

def dfs(x, a, b, c):
  if x == N:
    if min(a, b, c) >= 30:
      print(a, b, c)
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return float("inf")
  
  ret0 = dfs(x+1, a, b, c)
  ret1 = dfs(x+1, a+l[x], b, c)+10
  ret2 = dfs(x+1, a, b+l[x], c)+10
  ret3 = dfs(x+1, a, b, c+l[x])+10
  return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))

Fixed code:
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

def dfs(x, a, b, c):
  if x == N:
    if min(a, b, c) > 0:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return float("inf")
  
  ret0 = dfs(x+1, a, b, c)
  ret1 = dfs(x+1, a+l[x], b, c)+10
  ret2 = dfs(x+1, a, b+l[x], c)+10
  ret3 = dfs(x+1, a, b, c+l[x])+10
  return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))
Buggy code:
import sys
inp = sys.stdin.readline

N, A, B, C = map(int,inp().split())
L = [int(inp()) for _ in range(N)]

inf = float('inf')
def dfs(x, a, b, c):
  if(x == N):
    if(min(a, b, c) > 0):
      return abs(a-A) + abs(b-B) + abs(c-C) - 30
    else:
      return inf
  ret0 = dfs(x+1, a, b, c) + 10
  ret1 = dfs(x+1, a+L[x], b, c) + 10
  ret2 = dfs(x+1, a, b+L[x], c) + 10
  ret3 = dfs(x+1, a, b, c+L[x]) + 10
  return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))

Fixed code:
import sys
inp = sys.stdin.readline

N, A, B, C = map(int,inp().split())
L = [int(inp()) for _ in range(N)]

inf = float('inf')

def dfs(x, a, b, c, m):
  if(x == N):
    if(min(a, b, c) > 0):
      return abs(a-A) + abs(b-B) + abs(c-C) + (m-3) * 10
    else:
      return inf
  ret0 = dfs(x+1, a, b, c, m)
  ret1 = dfs(x+1, a+L[x], b, c, m+1)
  ret2 = dfs(x+1, a, b+L[x], c, m+1)
  ret3 = dfs(x+1, a, b, c+L[x], m+1)
  return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0, 0))
Buggy code:
N, A, B, C = map(int, input().split())

ABC = [A, B, C]

l = [int(input()) for i in range(N)]

abc = [0 for i in range(N)]

ans = 1 << 30

def cost():
    take = [0 for i in range(3)]

    tmp = 0

    for i in range(N):
        if abc[i] == 0:
            continue
        take[abc[i]-1] += l[i]
        tmp += 1

    tmp = (tmp - 3) * 10

    for i in range(3):
        tmp += abs(ABC[i] - take[i])

    return tmp


def dfs(i):
    global ans
    if i == N:
        ans = min(ans, cost())
        return
    for j in range(4):
        abc[i] = j
        dfs(i+1)

dfs(0)
print(ans)

Fixed code:
N, A, B, C = map(int, input().split())

ABC = [A, B, C]

l = [int(input()) for i in range(N)]

abc = [0 for i in range(N)]

ans = 1 << 30

def cost():
    take = [0 for i in range(3)]

    tmp = 0

    for i in range(N):
        if abc[i] == 0:
            continue
        take[abc[i]-1] += l[i]
        tmp += 1

    tmp = (tmp - 3) * 10


    for i in range(3):
        if take[i] == 0:
            return 1 << 30
        tmp += abs(ABC[i] - take[i])

    return tmp


def dfs(i):
    global ans
    if i == N:
        ans = min(ans, cost())
        return
    for j in range(4):
        abc[i] = j
        dfs(i+1)

dfs(0)
print(ans)
Buggy code:
n,a,b,c=map(int,input().split())
l=[]
for _ in range(n):
  x=int(input())
  l.append(x)

def dfs(cur,x,y,z):
  if cur==n and min(x,y,z)>0:
    return abs(a-x)+abs(b-y)+abs(c-z)-30
  elif cur==n:
    return inf
  ret0=dfs(cur+1,x,y,z)
  ret1=dfs(cur+1,x+l[cur],y,z)+10
  ret2=dfs(cur+1,x,y+l[cur],z)+10
  ret3=dfs(cur+1,x,y,z+l[cur])+10
  return min(ret0,ret1,ret2,ret3)

print(dfs(0,0,0,0))


Fixed code:
n,a,b,c=map(int,input().split())
l=[]
for _ in range(n):
  d=int(input())
  l.append(d)

def dfs(cur,x,y,z):
  if cur==n and min(x,y,z)>0:
    return abs(a-x)+abs(b-y)+abs(c-z)-30
  elif cur==n:
    return 100000000000000000000
  ret0=dfs(cur+1,x,y,z)
  ret1=dfs(cur+1,x+l[cur],y,z)+10
  ret2=dfs(cur+1,x,y+l[cur],z)+10
  ret3=dfs(cur+1,x,y,z+l[cur])+10
  return min(ret0,ret1,ret2,ret3)

print(dfs(0,0,0,0))
Buggy code:
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

for i in range(n):
    length.append(int(input()))


def DFS(cur, x, y, z):
    if cur == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(a, b,
                                                                c) else inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


print(DFS(0, 0, 0, 0))


Fixed code:
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

for i in range(n):
    length.append(int(input()))


def DFS(cur, x, y, z):
    if cur == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y,
                                                                z) else inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


print(DFS(0, 0, 0, 0))
Buggy code:
n,a,b,c=map(int,input().split())
L=[int(input()) for _ in range(n)]

res2=1000000000

for i in range(2**n-1):
	for j in range(2**n-1):
		x=[]
		y=[]
		z=[]
		for k in range(n):
			
			if i>>k&1:
				if j>>k&1:
					x.append(L[k])
				else:
					y.append(L[k])
			else:
				if j>>k&1:
					z.append(L[k])
			res1=abs(sum(x)-a)+abs(sum(y)-b)+abs(sum(z)-c)
			
			if len(x)>1:
				res1+=10*(len(x)-1)
			if len(y)>1:
				res1+=10*(len(y)-1)
			if len(z)>1:
				res1+=10*(len(z)-1)				
			
			res2=min(res1,res2)

print(res2)

Fixed code:
n,a,b,c=map(int,input().split())
L=[int(input()) for _ in range(n)]

res2=1000000000

for i in range(2**n-1):
	for j in range(2**n-1):
		x=[]
		y=[]
		z=[]
		for k in range(n):
			
			if i>>k&1:
				if j>>k&1:
					x.append(L[k])
				else:
					y.append(L[k])
			else:
				if j>>k&1:
					z.append(L[k])
		res1=abs(sum(x)-a)+abs(sum(y)-b)+abs(sum(z)-c)
		
		if len(x)==0 or len(y)==0 or len(z)==0:
			continue
	
		if len(x)>1:
			res1+=10*(len(x)-1)
		if len(y)>1:
			res1+=10*(len(y)-1)
		if len(z)>1:
			res1+=10*(len(z)-1)				
		res2=min(res1,res2)

print(res2)
Buggy code:
from itertools import product
N, a, b, c = map(int, input().split())
L = []
for i in range(N):
  L.append(int(input()))

abc = [0,a,b,c]
ch = [0,1,2,3]
cal = []

for i in product(ch,repeat=N):
  calc = 0
  su = [0,0,0,0]
  ct = [0,0,0,0]
  temp = list(i)
  for j in range(N):
    su[temp[j]] += L[j]
    ct[temp[j]] += 1
  for k in range(1,4):
    calc += abs(abc[k]-su[k])
    calc += max((ct[k]-1),0)*10
  cal.append(calc)
    
print(min(cal))

Fixed code:
from itertools import product
N, a, b, c = map(int, input().split())
L = []
for i in range(N):
  L.append(int(input()))

abc = [0,a,b,c]
ch = [0,1,2,3]
cal = []

for i in product(ch,repeat=N):
  calc = 0
  su = [0,0,0,0]
  ct = [0,0,0,0]
  temp = list(i)
  for j in range(N):
    su[temp[j]] += L[j]
    ct[temp[j]] += 1
  if (ct[1]==0)|(ct[2]==0)|(ct[3]==0):
    continue
  for k in range(1,4):
    calc += abs(abc[k]-su[k])
    calc += max((ct[k]-1),0)*10
  cal.append(calc)
    
print(min(cal))
Buggy code:
from itertools import permutations

def main():
    n, a, b, c = map(int, input().split())
    l = [int(input()) for _ in range(n)]

    # full search
    ans = 10**9
    for perm in permutations(l):
        cost = -30
        for i in range(1, n-1):
            cost1 = cost + 10 * len(perm[:i]) + abs(a - sum(perm[:i]))
            for j in range(i+1, n):
                cost2 = cost1 + 10 * len(perm[i:j]) + abs(b - sum(perm[i:j]))
                for k in range(j+1, n+1):
                    cost3 = cost2 + 10 * len(perm[j:k]) + abs(c - sum(perm[j:k]))
                    if cost3 < ans:
                        ans = cost3
                        print(perm[:i], perm[i:j], perm[j:k])

    print(ans)

main()

Fixed code:
from itertools import permutations

def main():
    n, a, b, c = map(int, input().split())
    l = [int(input()) for _ in range(n)]

    # full search
    ans = 10**9
    for perm in permutations(l):
        cost = -30
        for i in range(1, n-1):
            cost1 = cost + 10 * len(perm[:i]) + abs(a - sum(perm[:i]))
            for j in range(i+1, n):
                cost2 = cost1 + 10 * len(perm[i:j]) + abs(b - sum(perm[i:j]))
                for k in range(j+1, n+1):
                    cost3 = cost2 + 10 * len(perm[j:k]) + abs(c - sum(perm[j:k]))
                    if cost3 < ans:
                        ans = cost3

    print(ans)

main()