Problem:


Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243



Sample input:
5 100 90 80
98
40
30
21
80


Sample output:
23

Submission status: Runtime Error

Solution code:
import copy

N,A,B,C = map(int,raw_input().split())
take = [A,B,C]

l = [map(int,(raw_input()+' 0 0 0').split()) for i in range(N)]

ans = 0
flag = [0,0,0]
for i in range(N):
    l[i][1] = abs(A-l[i][0])
    l[i][2] = abs(B-l[i][0])
    l[i][3] = abs(C-l[i][0])

l.sort(key = lambda x:x[1])
if l[0][1] < 10:
    ans += l[0][1]
    flag[0] = 1
    del l[0]

l.sort(key = lambda x:x[2])
if l[0][2] < 10:
    ans += l[0][2]
    flag[1] = 1
    del l[0]

l.sort(key = lambda x:x[3])
if l[0][3] < 10:
    ans += l[0][3]
    flag[2] = 1
    del l[0]

for i in [2,1,0]:
    minimam = 10000000000000
    sayonara = []
    if flag[i] == 1:
        continue
    for j in xrange(2**len(l)-1):
        ret = -10
        cnt = 0
        length = 0
        data = []
        while cnt < len(l):
            if j%2 == 0: #siyou
                ret += 10
                length += l[cnt][0]
                data.append(1)
            else:
                data.append(0)
            j /= 2
            cnt += 1
        if minimam > abs(take[i]-length)+ret:
            minimam = abs(take[i]-length)+ret
            sayonara = copy.copy(data)
    for j in range(len(sayonara)):
        if sayonara[j] == 1:
            del l[j]
    ans += minimam
    
print ans

Submission status: Runtime Error

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

take_list_global = []
for _ in range(N):
    take_list_global.append(int(input()))


abc = [A, B, C]

if A in take_list_global:
    take_list_global.remove(A)
    acb.remove(A)

if B in take_list_global:
    take_list_global.remove(B)
    abc.remove(B)

if C in take_list_global:
    take_list_global.remove(C)
    abc.remove(C)

if len(abc) == 0:
    print(0)


def combination(n: int) -> list:
    c = []
    for i in range(1<<n):
        if i == 0:
            continue
        l = []
        for j in range(n):
            if i&(1<<j) != 0:
                l.append(j)
        c.append(l)
    return c

from itertools import permutations
tar_lists = permutations(abc)

ans_list = []

for tar_list in tar_lists:
    
    ans = 0
    take_list = take_list_global.copy()
    
    for tar in tar_list:
        cost = 0
        com_lists = combination(len(take_list))
        min_abs = 1001
        min_com = []
        
        for com_list in com_lists:
            s = 0
            for c in com_list:
                s += take_list[c]
            if min_abs > abs(tar - s):
                min_abs = abs(tar - s)
                min_com = com_list

        del_take = []
        
        for c in min_com:
            del_take.append(take_list[c])

        for d in del_take:
            take_list.remove(d)

        cost += 10 * (len(min_com) - 1)
        cost += min_abs
        ans += cost

    ans_list.append(ans)

print(min(ans_list))

Submission status: Runtime Error

Solution code:
(N,A,B,C)=map(int,input().split())
2
L=[]
3
for i in range(N):
4
  L.append(int(input()))
5
  
6
L_sums=[]
7
costs=[]
8
#bin_is=[]
9
for i in range(2**N+1,2**N+2**N):
10
  bin_i=str(bin(i))[2:]
11
  L_sum=0
12
  cost=-1
13
  for j in range(N):
14
    L_sum+=L[j]*int(bin_i[j+1])
15
    cost+=int(bin_i[j+1])
16
  L_sums.append(L_sum)
17
  costs.append(10*cost)
18
  #bin_is.append(bin_i)
19
​
20
cost_A=[]
21
for i in range(2**N-1):
22
  cost_A.append(costs[i]+abs(L_sums[i]-A))
23
min_cost_A=min(cost_A)
24
min_cost_A_index=cost_A.index(min(cost_A))+1
25
min_cost_A_index_bin=str(bin(min_cost_A_index+2**N))[2:]
26
for i in range(N):
27
  if min_cost_A_index_bin[i+1]=='1':
28
    del(L[i])
29
N=len(L)
30
#-------------------
31
L_sums=[]
32
costs=[]
33
#bin_is=[]
34
for i in range(2**N+1,2**N+2**N):
35
  bin_i=str(bin(i))[2:]
36
  L_sum=0
37
  cost=-1
38
  for j in range(N):
39
    L_sum+=L[j]*int(bin_i[j+1])
40
    cost+=int(bin_i[j+1])
41
  L_sums.append(L_sum)
42
  costs.append(10*cost)
43
  #bin_is.append(bin_i)
44
​
45
cost_B=[]
46
for i in range(2**N-1):
47
  cost_B.append(costs[i]+abs(L_sums[i]-B))
48
min_cost_B=min(cost_B)
49
min_cost_B_index=cost_B.index(min(cost_B))+1
50
min_cost_B_index_bin=str(bin(min_cost_B_index+2**N))[2:]
51
for i in range(N):
52
  if min_cost_B_index_bin[i]=='1':
53
    del(L[i])
54
N=len(L)
55
#-----------------
56
L_sums=[]
57
costs=[]
58
#bin_is=[]
59
for i in range(2**N+1,2**N+2**N):
60
  bin_i=str(bin(i))[2:]
61
  L_sum=0
62
  cost=-1
63
  for j in range(N):
64
    L_sum+=L[j]*int(bin_i[j+1])
65
    cost+=int(bin_i[j+1])
66
  L_sums.append(L_sum)
67
  costs.append(10*cost)
68
  #bin_is.append(bin_i)
69
​
70
cost_C=[]
71
for i in range(2**N-1):
72
  cost_C.append(costs[i]+abs(L_sums[i]-C))
73
min_cost_C=min(cost_C)
74
#min_cost_C_index=cost_C.index(min(cost_C))+1
75
#min_cost_C_index_bin=str(bin(min_cost_C_index))[2:]
76
#for i in range(N):
77
#  if min_cost_C_index_bin[i]=='1':
78
#    del(L[i])
79
#N=len(L)
80
#----------------
81
print(min_cost_A+min_cost_B+min_cost_C)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest 119：C - Synthetic Kadomatsu


N, A, B, C = [int(i) for i in raw_input().strip().split(' ')]


def bitcount(n):
    nb = bin(n)[2:]
    return nb.count('1')


def suml(l, n):
    s = 0
    for i in range(N):
        s += l[i] * (n&1)
        n >>= 1
    return s


def main():

    l = []
    for _ in range(N):
        h = int(raw_input().strip())
        l.append(h)
    # l.sort(reverse=False)

    cand = []
    for i in range(2**N):
        cand.append((i, suml(l, i), bitcount(i)))
    # print cand

    '''
    # C
    mincmp = 10000000000000
    mincn = 0
    for c in cand:
        if c[2] <= N-2:
            mp = abs(C-c[1]) + 10*(c[2]-1)
            if mp < mincmp:
                mincmp = mp
                mincn = c[0]

    # B
    minbmp = 10000000000000
    minbn = 0
    for c in cand:
        if c[2] <= N-1 and c[0]&mincn==0:
            mp = abs(B-c[1]) + 10*(c[2]-1)
            if mp < minbmp:
                minbmp = mp
                minbn = c[0]
                print c, mp

    # A
    minamp = 10000000000000
    minan = 0
    for c in cand:
        if c[2] <= N and c[0]&(mincn|minbn)==0:
            mp = abs(A-c[1]) + 10*(c[2]-1)
            if mp < minamp:
                minamp = mp
                minan = c[0]
                print c, mp
    '''

    minmp = 10000000000000
    for a in range(1, N**2):
        if cand[a][2] <= N-2:
            for b in range(1, N**2):
                if cand[b][2] <= N-1 and cand[a][0]&cand[b][0]==0:
                    for c in range(1, N**2):
                        if cand[c][2] <= N and cand[c][0]&(cand[a][0]|cand[b][0]) == 0:
                            mp = abs(A-cand[a][1]) + 10*(cand[a][2]-1) + abs(B-cand[b][1]) + 10*(cand[b][2]-1) + abs(C-cand[c][1]) + 10*(cand[c][2]-1)
                            if mp < minmp:
                                minmp = mp
                                # print a, b, c
    print minmp


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

N, A, B, C =map(int,input().split())
l = []
for i in range(N):
    a = input()
    l.append(a)

def DFS(i,uselist0,uselista,uselistb,uselistc):
    if i == N:
        uselist = [uselist0,uselista,uselistb,uselistc]
        return calc_mp(uselist)
    mplist = []
    u0 = uselist0 + [l[i]]
    ua = uselista + [l[i]]
    ub = uselistb + [l[i]]
    uc = uselistc + [l[i]]
    mplist.append(DFS(i+1, u0,uselista,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,ua,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,ub,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,uselistb,uc))
    return min(mplist)


def calc_mp(uselist):
    mp = 0

    #合成魔法
    mp += (max(len(uselist[1]) - 1, 0)) * 10
    mp += (max(len(uselist[2]) - 1, 0)) * 10
    mp += (max(len(uselist[3]) - 1, 0)) * 10

    #その他魔法
    mp += abs(A - sum(uselist[1]))
    mp += abs(B - sum(uselist[2]))
    mp += abs(C - sum(uselist[3]))

    return mp

print(DFS(0,[],[],[],[]))

Submission status: Runtime Error

Solution code:
N = list(map(int,input().split()))
l = [int(input()) for i in range(N[0])]

if N[1] in l:
    l.remove(A)
    del N[1]
if N[2] in l:
    l.remove(B)
    del N[2]
if N[3] in l:
    l.remove(C)
    del N[3]

MP=0

for i in N[1:]:
    P1 = min([abs(x-i) for x in l])
    P2 = min([abs(x+y-i) for x in l for y in l if l.index(x)!=l.index(y)])
    if P1>=P2:
        MP+=P1
    else:
        MP+=P2

print(MP)

Submission status: Runtime Error

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

def calc_cost(num, group):
    cost = 10 * (group - 1) + abs(sum(group) - num)
    return cost

cand = []
for n in range(4 ** N):
    groups = [[], [], []]
    for i in range(N):
        temp = n % 4
        if n > 0:
            groups[temp-1].append(i)
        n = n // 4
    if len(group[0]) * len(group[1]) * len(group[2]) > 0: 
        cand.append(calc_cost(A, group[0]) + calc_cost(B, group[1]) + calc_cost(C, group[2]))
print(min(cand))

Submission status: Runtime Error

Solution code:

now_ans = 100000


def kotae(A, a_tree, a_tree_sum):
    ans = (a_tree - 1) * 10 + abs(a_tree_sum - A)
    if a_tree == 0:
        ans = 100000
    return ans

def calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum):
    global now_ans
    ans = 0
    ans += kotae(A, a_tree, a_tree_sum)
    ans += kotae(B, b_tree, b_tree_sum)
    ans += kotae(C, c_tree, c_tree_sum)
    if ans < now_ans:
        now_ans = ans

def calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index, a_tree_sum, b_tree_sum, c_tree_sum ):
    if a_tree_sum >= A and b_tree_sum >= B and c_tree_sum >= C:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if index == len(tree_data) - 1 or tree_data[i + 1] <= 10:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if a_tree_sum < A:
        calc_data(tree_data, A, B, C, a_tree + 1, b_tree, c_tree, index + 1, a_tree_sum + tree_data[index + 1], b_tree_sum, c_tree_sum)
    if b_tree_sum < B:
        calc_data(tree_data, A, B, C, a_tree, b_tree + 1, c_tree, index + 1, a_tree_sum, b_tree_sum + tree_data[index + 1], c_tree_sum)

    if c_tree_sum < C:
        calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree + 1, index + 1, a_tree_sum, b_tree_sum, c_tree_sum + tree_data[index + 1])

    calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index + 1, a_tree_sum, b_tree_sum, c_tree_sum)


def main():
    global now_ans
    num, A, B, C = list(map(int, input().split()))
    tree_data = [int(input()) for i in range(num)]
    tree_data.sort(reverse=True)

    calc_data(tree_data, A, B, C, 1, 0, 0, 0, tree_data[0], 0, 0)
    calc_data(tree_data, A, B, C, 0, 1, 0, 0, 0, tree_data[0], 0)
    calc_data(tree_data, A, B, C, 0, 0, 1, 0, 0, 0, tree_data[0])
    calc_data(tree_data, A, B, C, 0, 0, 0, 0, 0, 0, 0)

    print(now_ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
cnt = 0
for k in [a,b,c]:
    ans = 0
    for i in range(1, 257):
        s = 0
        for j in range(7):
            if i & 2**j:
                if s > 0:
                    s += 10
                s += l[j]
        v = abs(k - s)
        if v < ans:
            ans = v
            x = i
    cnt += ans
    for j in range(7):
        if x & 2**j:
            l[j] = 0
print(cnt)

Submission status: Runtime Error

Solution code:
import bisect
import itertools

n, a, b, c = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
ABC = [a, b, c]
ans = float("inf")

def dfs(i, abc):
    global ans
    sum_a = 0
    l1 = l[:]
    if i == 3:
        for k in itertools.permutations([0, 1, 2], 3):
            for j in k:
                index = bisect.bisect_left(l1, ABC[j])
                if abc[j] == 0:
                    if index < len(l1):
                        sum_a += l1[index] - ABC[j]
                        l1.remove(l1[index])
                    else:
                        sum_a += float("inf")
                elif abc[j] == 1:
                    if index > 0:
                        sum_a += ABC[j] - l1[index - 1]
                        l1.remove(l1[index - 1])
                    else:
                        sum_a += float("inf")
                else:
                    sum_ = float("inf")
                    for x in range(2, len(l1) + 1):
                        for y in itertools.combinations(l1, x):
                            if sum_ > abs(sum(y) - ABC[j]):
                                sum_ = abs(sum(y) - ABC[j]) + (x - 1) * 10
                                l2 = l1[:]
                                for z in y:
                                    l2.remove(z)
                    l1 = l2[:]
                    sum_a += sum_
            ans = min(ans, sum_a)
        return 0
    dfs(i + 1, abc[:i] + [abc[i]] + abc[i + 1:])
    dfs(i + 1, abc[:i] + [abc[i] + 1] + abc[i + 1:])
    dfs(i + 1, abc[:i] + [abc[i] + 2] + abc[i + 1:])

dfs(0, [0, 0, 0])
print(ans)

Submission status: Runtime Error

Solution code:
N = list(map(int,input().split()))
l = [int(input()) for i in range(len(N))]

if N[1] in l:
    l.remove(A)
    del N[1]
if N[2] in l:
    l.remove(B)
    del N[2]
if N[3] in l:
    l.remove(C)
    del N[3]

MP=0

for i in N[1:]:
    P1 = min([abs(x-i) for x in l])
    P2 = min([abs(x+y-i) for x in l for y in l if index(x)!=index(y)])
    if P1>=P2:
        MP+=P1
    else:
        MP+=P2

print(MP)

Submission status: Runtime Error

Solution code:
INF = int(1e20)

def solve(s, l):
    n = len(l)
    a = s[0]
    ret = INF
    if n == 0: return INF
    else:
        ll = []
        for bit in xrange(1, 1 << n):
            st = ans = 0
            for j in xrange(n):
                if (bit & (1 << j)):
                    st += l[j]
                    ans += 10
                else:
                    ll.append(l[j])
            ans -= 10
            ans += abs(a - st)
            if n > 1:
                ans = ans + f(s[1:], ll)
            ret = min(ret, ans)

    return ret

n, a, b, c = map(int, raw_input().strip().split())
l = [0 for i in xrange(n)]
for i in xrange(n): l[i] = input()

ans = solve([a, b, c], l)
ans = min(ans, solve([a, c, b], l))
ans = min(ans, solve([b, a, c], l))
ans = min(ans, solve([b, c, a], l))
ans = min(ans, solve([c, a, b], l))
ans = min(ans, solve([c, b, a], l))

print ans

Submission status: Runtime Error

Solution code:
NABC = input().split()

N = int(NABC[0])
ABC = []
ABC.append( int(NABC[1]) )
ABC.append( int(NABC[2]) )
ABC.append( int(NABC[3]) )
MP = 0

Tlist = []
for lop in range(N):
    l = int(input())
    Tlist.append(l)

Tlist = sorted(Tlist)

if ABC[2] in Tlist:
    Tlist.pop(Tlist.index(ABC[2]))
    ABC.pop(2)
if ABC[1] in Tlist:
    Tlist.pop(Tlist.index(ABC[1]))
    ABC.pop(1)
if ABC[0] in Tlist:
    Tlist.pop(Tlist.index(ABC[0]))
    ABC.pop(0)

if len(ABC) == 3:
    if ABC[2] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[2])
        ABC.pop(2)
        Tlist.pop(0)
if len(ABC) >= 2:
    if ABC[1] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[1])
        ABC.pop(1)
        Tlist.pop(0)
if len(ABC) >= 1:
    if ABC[0] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[0])
        ABC.pop(0)
        Tlist.pop(0)
while(True):
    if ABC == []:
        break
    take = ABC[0]
    if take < Tlist[0]:
        MP = MP + (Tlist[0] - take)
        ABC.pop(0)
        Tlist.pop(0)
    else:
        sa = 10000
        Tindex = -1
        for lop in range(len(Tlist)):
            if abs(take - Tlist[lop]) < sa:
                Tindex = lop
                sa = abs(take - Tlist[lop])
        if sa < 10:
            Tlist.pop(Tindex)
            ABC.pop(0)
            MP = MP + sa
        else:
            sa2 = 10000
            maeI = -1
            atoI = -1
            for lop in range(len(Tlist)):
                mae = Tlist[lop]
                for lop2 in range(len(Tlist)):
                    if not(lop == lop2):
                        ato = Tlist[lop2]
                        if abs(take - (mae + ato)) < sa2:
                            maeI = lop
                            atoI = lop2
                            sa2 = abs(take - (mae + ato))
            if sa > sa2:
                MP = MP + 10
                if maeI > atoI:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(maeI)
                    Tlist.pop(atoI)
                    Tlist = sorted(Tlist)  
                else:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(atoI)
                    Tlist.pop(maeI)
                    Tlist = sorted(Tlist)
            else:
                Tlist.pop(Tindex)
                ABC.pop(0)
                MP = MP + sa
    
print(MP)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
N, A, B, C = map(int, input().split())

bamboos = []

for i in range(N):
    bamboos.append(int(input()))

def bamboo(bamboos, A):
    total = 0 
    while(len(bamboos) >= 1):
        #print(bamboos)
        A_i = 0
        A_c = abs(A - bamboos[0])
        A_u = bamboos[0]

        for i in range(1, len(bamboos)):
            if A_c > abs(A - bamboos[i]):
                A_c = abs(A - bamboos[i])
                A_i = i
                A_u = bamboos[i]

        if A_c <= 10:
            total += A_c
            #print("bamboos" + str(bamboos[A_i]))
            del bamboos[A_i]
            return total
        else:
            A_c = math.inf
            iteration = 0
            A_i = 0
            A_j = 0
            for i in range(0,len(bamboos)):
                for j in range(i+1,len(bamboos)):
                    if i != j and A_c > abs(A - (bamboos[i] + bamboos[j])):
                        A_c = abs(A - (bamboos[i] + bamboos[j]))
                        A_i = i
                        A_j = j
            total += 10
            val = bamboos[A_i] + bamboos[A_j]
            #print('i' + str(bamboos[i]))
            #print('j' + str(bamboos[j]))
            #print(str(i) + ',' + str(j))
            if (A_i < A_j):
                del bamboos[A_j]
                del bamboos[A_i]
            elif (A_j < A_i):
                del bamboos[A_i]
                del bamboos[A_j]
                iteration += 1
            bamboos.append(val)
            #print(total)

    #return total

bamboos_b = bamboos.copy()

to_solve = [A,B,C]
#to_solve.sort()

#print(to_solve)

total = []

for perm in list(itertools.permutations(to_solve)):
    bamboos = bamboos_b.copy()
    t = 0
    #print()
    #print()
    #print(perm)
    for elem in perm:
        #print(bamboos)
        t += bamboo(bamboos, elem)
        #print(t)
    total.append(t)
        
print(min(total))

Submission status: Runtime Error

Solution code:
NABC = input().split()

N = int(NABC[0])
ABC = []
ABC.append( int(NABC[1]) )
ABC.append( int(NABC[2]) )
ABC.append( int(NABC[3]) )
MP = 0

Tlist = []
for lop in range(N):
    l = int(input())
    Tlist.append(l)

Tlist = sorted(Tlist)

if ABC[2] in Tlist:
    Tlist.pop(Tlist.index(ABC[2]))
    ABC.pop(2)
if ABC[1] in Tlist:
    Tlist.pop(Tlist.index(ABC[1]))
    ABC.pop(1)
if ABC[0] in Tlist:
    Tlist.pop(Tlist.index(ABC[0]))
    ABC.pop(0)

if len(ABC) == 3:
    if ABC[2] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[2])
        ABC.pop(2)
        Tlist.pop(0)
if len(ABC) >= 2:
    if ABC[1] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[1])
        ABC.pop(1)
        Tlist.pop(0)
if len(ABC) >= 1:
    if ABC[0] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[0])
        ABC.pop(0)
        Tlist.pop(0)
while(True):
    if ABC == []:
        break
    take = ABC[0]
    if take < Tlist[0]:
        MP = MP + (Tlist[0] - take)
        ABC.pop(0)
        Tlist.pop(0)
    else:
        sa = 10000
        Tindex = -1
        for lop in range(len(Tlist)):
            if abs(take - Tlist[lop]) < sa:
                Tindex = lop
                sa = abs(take - Tlist[lop])
        if sa < 10:
            Tlist.pop(Tindex)
            ABC.pop(0)
            MP = MP + sa
        else:
            sa2 = 10000
            maeI = -1
            atoI = -1
            for lop in range(len(Tlist)):
                mae = Tlist[lop]
                for lop2 in range(len(Tlist)):
                    if not(lop == lop2):
                        ato = Tlist[lop2]
                        if abs(take - (mae + ato)) < sa2:
                            maeI = lop
                            atoI = lop2
                            sa2 = abs(take - (mae + ato))
            if sa > sa2:
                MP += 10
                if maeI > atoI:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(maeI)
                    Tlist.pop(atoI)
                    Tlist = sorted(Tlist)  
                else:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(atoI)
                    Tlist.pop(maeI)
                    Tlist = sorted(Tlist)
            else:
                Tlist.pop(Tindex)
                ABC.pop(0)
                MP = MP + sa
    
print(MP)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

def update(df, ol, t):
  ol = ol[ol!=10000].reshape(-1, 3)
  df = df[df!=10000].reshape(-1, 3)
  print(ol)
  print(df)
  for idx in range(ol.shape[1]):
    min_a = np.argmin(ol[:,idx])
    max_a = np.argmax(ol[:,idx])
    ol[max_a] = ol[min_a] + ol[max_a]
    ol[min_a] = 0
  ol = ol[ol!=0].reshape(-1,3)
  df = t - ol
  return df, ol

v = []
for line in sys.stdin:
    v.append([int(i) for i in line.split()])

a, b, c = v[0][1:]
t = [a, b, c]
l = np.squeeze(np.array(v[1:]))
ol = np.zeros((3, len(l)))
ol[0] = l
ol[1] = l
ol[2] = l
ol = ol.transpose()
df = np.zeros((3, len(l)))
df[0] = np.array([a] * len(l)) - l
df[1] = np.array([b] * len(l)) - l
df[2] = np.array([c] * len(l)) - l
df = df.transpose()
count = 0
mp = 0
while count != 3:
  for idx, df_v in enumerate(df):
    min_v = np.min(np.abs(df_v))
    min_v_arg = np.argmin(np.abs(df_v))
    
    if min_v <= 10:
      ol[idx,:] = 10000
      df[idx,:] = 10000
      t[min_v_arg] = -1
      count += 1
      mp += min_v
      if count == 3:
        break
  if count == 3:
    break
  df, ol = update(df, ol, t)
  mp += 10
print(np.int(mp))

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
l=[]
for i in range(N):
  l.append(int(input()))
l_sum =[]
l_sum.append(l)
for h in range(1,N):
  l_sum_temp = []
  for i in range(N):
    for j in range(N):
      l_sum_temp.append(l_sum[h-1][i] + l_sum[h-1][j])
  l_sum.append(list(set(l_sum_temp)))
MP_list_A =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_A = abs(A - l_sum[h][i]) + h*10
    MP_list_A.append(MP_A)
MP = min(MP_list_A)
MP_list_B =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_B = abs(B - l_sum[h][i]) + h*10
    MP_list_B.append(MP_B)
MP += min(MP_list_B)
MP_list_C =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_C = abs(C - l_sum[h][i]) + h*10
    MP_list_C.append(MP_C)
MP += min(MP_list_C)
print(MP)

Submission status: Runtime Error

Solution code:
a, b, q = list(map(int, input().strip().split()))
s = []
t = []
for i in range(a):
    s.append(int(input()))

for i in range(b):
    t.append(int(input()))

def binsearch_left_index(x, s, l, r):
    min_s = s[0]
    max_s = s[-1]
    if x < min_s:
        return None

    if x > max_s:
        return -1

    if r - l <= 1:
        return l

    if s[l] == x:
        return l;

    c = (l + r) // 2
    if s[c] > x:
        return binsearch_left_index(x, s, l, c)
    else:
        return binsearch_left_index(x, s, c, r)

def binsearch_right_index(x, s, l, r):
    min_s = s[0]
    max_s = s[-1]
    if x < min_s:
        return 0

    if x > max_s:
        return None

    return binsearch_left_index(x, s, l, r) + 1

def search(x):
    l_shrine = binsearch_left_index(x, s, 0, len(s))
    l_temple = binsearch_left_index(x, t, 0, len(t))
    r_shrine = binsearch_right_index(x, s, 0, len(s))
    r_temple = binsearch_right_index(x, t, 0, len(t))
# 左左と行く場合
    if l_shrine is None or l_temple is None:
        ll = 1e20
    else:
        ll = max(abs(s[l_shrine] - x), abs(t[l_temple] - x))

# 左神社右寺と行く場合
    if l_shrine is None or r_temple is None:
        lr1 = 1e20
    else:
        lr1 = abs(s[l_shrine] - x) + abs(t[r_temple] - s[l_shrine])

# 左寺右神社と行く場合
    if l_temple is None or r_shrine is None:
        lr2 = 1e20
    else:
        lr2 = abs(t[l_temple] - x) + abs(s[r_shrine] - l_temple)

# 右神社左寺と行く場合
    if r_shrine is None or l_temple is None:
        rl1 = 1e20
    else:
        rl1 = abs(s[r_shrine] - x) + abs(t[l_temple] - s[r_shrine])

# 右寺左神社と行く場合
    if l_shrine is None or r_temple is None:
        rl2 = 1e20
    else:
        rl2 = abs(t[r_temple] - x) + abs(s[l_shrine] - t[r_temple])

# 右右と行く場合
    if r_shrine is None or r_temple is None:
        rr = 1e20
    else:
        rr = max(abs(s[r_shrine] - x), abs(t[r_temple] - x))


    ans = min(ll, lr1, lr2, rl1, rl2, rr)
    return ans

for i in range(q):
    x = int(input())
    print(search(x))

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
l=[]
for i in range(N):
  l.append(int(input()))
l_sum =[]
l_sum.append(l)
for h in range(1,N):
  l_sum_temp = []
  for i in range(N):
    for j in range(N):
      l_sum_temp.append(l_sum[h-1][i] + l_sum[h-1][j])
  l_sum.append(set(l_sum_temp))
MP_list_A =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_A = abs(A - l_sum[h][i]) + h*10
    MP_list_A.append(MP_A)
MP = min(MP_list_A)
MP_list_B =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_B = abs(B - l_sum[h][i]) + h*10
    MP_list_B.append(MP_B)
MP += min(MP_list_B)
MP_list_C =[]
for h in range(N):
  for i in range(len(l_sum[h])):              
    MP_C = abs(C - l_sum[h][i]) + h*10
    MP_list_A.append(MP_C)
MP += min(MP_list_C)
print(MP)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

def update(df, ol, t):
  ol = ol[ol!=10000].reshape(-1, 3)
  df = df[df!=10000].reshape(-1, 3)
  for idx in range(len(df)):
    min_a = np.argmin(ol[:,idx])
    max_a = np.argmax(ol[:,idx])
    ol[max_a] = ol[min_a] + ol[max_a]
    ol[min_a] = 0
  ol = ol[ol!=0].reshape(-1,3)
  df = t - ol
  return df, ol

v = []
for line in sys.stdin:
    v.append([int(i) for i in line.split()])

a, b, c = v[0][1:]
t = [a, b, c]
l = np.squeeze(np.array(v[1:]))
ol = np.zeros((3, len(l)))
ol[0] = l
ol[1] = l
ol[2] = l
ol = ol.transpose()
df = np.zeros((3, len(l)))
df[0] = np.array([a] * len(l)) - l
df[1] = np.array([b] * len(l)) - l
df[2] = np.array([c] * len(l)) - l
df = df.transpose()
count = 0
mp = 0
while count != 3:
  for idx, df_v in enumerate(df):
    min_v = np.min(np.abs(df_v))
    min_v_arg = np.argmin(np.abs(df_v))
    
    if min_v <= 10:
      ol[idx,:] = 10000
      df[idx,:] = 10000
      t[min_v_arg] = -1
      count += 1
      mp += min_v
      if count == 3:
        break
  if count == 3:
    break
  df, ol = update(df, ol, t)
  mp += 10
print(np.int(mp))

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
l=[]
for i in range(N):
  l.append(int(input()))
l_sum =[]
l_sum.append(l)
for h in range(1,N):
  l_sum_temp = []
  for i in range(N):
    for j in range(N):
      l_sum_temp.append(l_sum[h-1][i] + l_sum[h-1][j])
  l_sum.append(set(l_sum_temp))
MP_list_A =[]
for h in range(N):
  for i in range(len(l_sum[h]):              
    MP_A = abs(A - l_sum[h][i]) + h*10
    MP_list_A.append(MP_A)
MP = min(MP_list_A)
MP_list_B =[]
for h in range(N):
  for i in range(len(l_sum[h]):              
    MP_B = abs(B - l_sum[h][i]) + h*10
    MP_list_B.append(MP_B)
MP += min(MP_list_B)
MP_list_C =[]
for h in range(N):
  for i in range(len(l_sum[h]):              
    MP_C = abs(C - l_sum[h][i]) + h*10
    MP_list_A.append(MP_C)
MP += min(MP_list_C)
print(MP)

Submission status: Runtime Error

Solution code:
N = list(map(int, input().split()))
bamboo =[input().rstrip() for i in range(int(N[0]))]
N_sorted = sorted(N[1:])
MP = 0
for i in range(len(bamboo)):
    for j in range(len(N_sorted)):
        if int(bamboo[i]) == N_sorted[j]:
            del N_sorted[j]
if len(N_sorted) == 0:
    print(MP)

Submission status: Runtime Error

Solution code:
N = list(map(int,input().split()))
l = [int(input()) for i in range(N)]

if N[1] in l:
    l.remove(A)
    del N[1]
if N[2] in l:
    l.remove(B)
    del N[2]
if N[3] in l:
    l.remove(C)
    del N[3]

MP=0

for i in N[1:]:
    P1 = min([abs(x-i) for x in l])
    P2 = min([abs(x+y-i) for x in l for y in l if index(x)!=index(y)])
    
    if P1>=P2:
        MP+=P1
        else:
            MP+=P2