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: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
L = [int(input()) for _ in range(n)]
mp = 0
def use(n, d, r):
  bit = [[0]*n for i in range(2**n)]
  for i in range(2**n):
    for j in range(n):
      if i&(2**j) > 0:
        bit[i][j] = 1
  cl = []
  for i in range(2**n):
    s = 0
    for j in range(n):
      s += L[j] * bit[i][j]
    cl.append(s)
  cmin = 999999
  for i in range(1, 2**n):
    if abs(cl[i] - d) < cmin and sum(bit[i]) + r <= n:
      cmin = abs(cl[i] - d)
      cindex = i
  ng = []
  use = []
  for j in range(n):
    if bit[cindex][j]:
      use.append(L[j])
      if L[j] < 10:
        ng.append(L[j])
  for x in use:
    L.remove(x)
  if sum(bit[cindex]) == 1:
     return cmin
  else:
    return sum(bit[cindex]) * 10 - 10 + cmin - 10 * len(ng) + sum(ng)
mp += use(n, c, 2)
n = len(L)
mp += use(n, b, 1)
n = len(L)
mp += use(n, a, 0)
print(mp)

Submission status: Wrong Answer

Solution code:
import itertools

def rotate(l, n):
    return l[-n:] + l[:-n]

def main():
    N, A, B, C = list(map(int, input().split()))
    bars = [A,B,C]
    l = []
    for i in range(N):
        l.append(int(input()))
        if l[-1] in bars:
            j = bars.index(l[-1])
            del bars[j]
            del l[-1]

    # print(bars, l)
    all_min = 10000000000

    for z in range(len(bars)):
        ret = 0
        bars = rotate(bars, 1)
        for k in range(len(bars)):
            full = []
            for i in range(1, N-len(bars[k:])+1):
                full += list(itertools.combinations(l, i))
            # print(full)


            mini = 1000000000
            arg_min = []
            for i in full:
                if abs(sum(i)-bars[k]) < mini:
                    mini = abs(sum(i)-bars[k])
                    arg_min = i
            
            # print(mini, arg_min)
            ret += 10*(len(arg_min)-1) if len(arg_min)!=0 else 0
            ret += mini
            for i in arg_min:
                del l[l.index(i)]

        all_min = min(all_min, ret)
    print(all_min) 

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

N = int(NABC[0])
ABC = []
ABC.append( int(NABC[3]) )
ABC.append( int(NABC[2]) )
ABC.append( int(NABC[1]) )
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)

ABC = sorted(ABC)
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 = 1000
        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 + 10:
                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: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

li = list(map(int,input().split()))
N, A, B, C = li[0], li[1], li[2], li[3]
l = sorted([ int(input()) for i in range(N) ])
an_l = sorted([A, B, C])

ans = 0
# for l_n_ind, l_n in enumerate(an_l):
#     score = 10
#     l_i_ind = None
#     for ind, l_i in enumerate(l):
#         tmp_score = abs(l_n-l_i)
#         if tmp_score < score:
#             score = tmp_score
#             l_i_ind = ind
#     if l_i_ind is not None:
#         ans += score
#         l.pop(l_i_ind)
#         an_l.pop(l_n_ind)

# an_sum = sum(an_l)
# d = { i: 999999 for i in range(li)}

for l_n_ind, l_n in enumerate(an_l):
    score = 99999
    l_i_inds = []
    for i in range(1 << len(l)):
        output = []
        output_ind = []
        l_i_ind = None
        for j in range(len(l)):
            if ((i >> j) & 1) == 1:
                output.append(l[j])
                output_ind.append(j)
        if len(output) == 0:
            continue
        tmp_score = abs(sum(output)-l_n)
        if tmp_score < score:
            score = tmp_score
            l_i_inds = output_ind

    ans += (score + (len(l_i_inds)-1)*10)
    for tt in l_i_inds[::-1]:
        l.pop(tt)

# print(an_l)
# print(l)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import sys

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

ABC_list = [A,B,C]
ABC_list.sort()

l_list = []
for i in range(N):
    l_list.append(int(input()))
l_list.sort()

MP = float('inf')

for i in range(len(ABC_list),len(l_list)+1):
    pena = (i - 3)*10
    cs = itertools.combinations(range(1,i),2)
    for c in cs:
        ps = itertools.combinations(l_list,i)
        for p in ps:
            AA = sum(p[:c[0]])
            BB = sum(p[c[0]:c[1]])
            CC = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp
        for p in ps:
            AA = sum(p[:c[0]])
            CC = sum(p[c[0]:c[1]])
            BB = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp

        for p in ps:
            BB = sum(p[:c[0]])
            AA = sum(p[c[0]:c[1]])
            CC = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp
        for p in ps:
            BB = sum(p[:c[0]])
            CC = sum(p[c[0]:c[1]])
            AA = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp

        for p in ps:
            CC = sum(p[:c[0]])
            AA = sum(p[c[0]:c[1]])
            BB = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp

        for p in ps:
            CC = sum(p[:c[0]])
            BB = sum(p[c[0]:c[1]])
            AA = sum(p[c[1]:])
            test = [AA,BB,CC]
            test.sort()
            tmp = abs(test[0]-ABC_list[0]) + abs(test[1]-ABC_list[1]) + abs(test[2]-ABC_list[2]) + pena
            if tmp < MP:
                MP = tmp
print(MP)

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

li = list(map(int,input().split()))
N, A, B, C = li[0], li[1], li[2], li[3]
l = sorted([ int(input()) for i in range(N) ])
an_l = sorted([A, B, C])

ans = 0
for l_n_ind, l_n in enumerate(an_l):
    score = 10
    l_i_ind = None
    for ind, l_i in enumerate(l):
        tmp_score = abs(l_n-l_i)
        if tmp_score < score:
            score = tmp_score
            l_i_ind = ind
    if l_i_ind is not None:
        ans += score
        l.pop(l_i_ind)
        an_l.pop(l_n_ind)

# an_sum = sum(an_l)
# d = { i: 999999 for i in range(li)}

for l_n_ind, l_n in enumerate(an_l):
    score = 99999
    l_i_inds = []
    for i in range(1 << len(l)):
        output = []
        output_ind = []
        l_i_ind = None
        for j in range(len(l)):
            if ((i >> j) & 1) == 1:
                output.append(l[j])
                output_ind.append(j)
        if len(output) == 0:
            continue
        tmp_score = abs(sum(output)-l_n)
        if tmp_score < score:
            score = tmp_score
            l_i_inds = output_ind

    ans += (score + (len(l_i_inds)-1)*10)
    for tt in l_i_inds[::-1]:
        l.pop(tt)

# print(an_l)
# print(l)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

li = list(map(int,input().split()))
N, A, B, C = li[0], li[1], li[2], li[3]
l = sorted([ int(input()) for i in range(N) ])
an_l = sorted([A, B, C])

ans = 0
for l_n_ind, l_n in enumerate(an_l):
    score = 10
    l_i_ind = None
    for ind, l_i in enumerate(l):
        tmp_score = abs(l_n-l_i)
        if tmp_score < score:
            score = tmp_score
            l_i_ind = ind
    if l_i_ind is not None:
        ans += score
        l.pop(l_i_ind)
        an_l.pop(l_n_ind)

# an_sum = sum(an_l)
# d = { i: 999999 for i in range(li)}

for l_n_ind, l_n in enumerate(an_l):
    score = 99999
    l_i_inds = []
    for i in range(1 << len(l)):
        output = []
        output_ind = []
        l_i_ind = None
        for j in range(len(l)):
            if ((i >> j) & 1) == 1:
                output.append(l[j])
                output_ind.append(j)
        if len(output) == 0:
            continue
        tmp_score = abs(sum(output)-l_n)
        if tmp_score < score:
            score = tmp_score
            l_i_inds = output_ind

    ans += (score + (len(l_i_inds)-1)*10)
    print("l: {}".format(l_n))
    print("ls: ")
    for k in l_i_inds:
        print(l[k])
    for tt in l_i_inds[::-1]:
        l.pop(tt)

# print(an_l)
# print(l)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[2] - a) < 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])

            while True:
                if i >= n:
                    break
                if abs(take[1] - b) < 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while True:
                if i >= n:
                    break
                if abs(take[0] - c) < 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()

Submission status: Wrong Answer

Solution code:
import numpy as np
n, a, b, c = (int(i) for i in input().split())
l = [int(input()) for i in range(n)]
if n == 3:
  l = np.array(l)
  lc = np.min(abs(l-c))
  lb = np.min(abs(l-b))
  la = np.min(abs(l-a))
  ans = lc+lb+la
else:
  ans = 100
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = [int(i) for i in input().split(' ')]
cba = [c,b,a]
banboo = []
for i in range(n):
  banboo.append(int(input()))
banboo.sort(reverse=True)

used = [False for i in range(n)]
answer=0;
for x in cba:
  if(banboo.count(x)>0):
    banboo.remove(x)
  else:
    for b in banboo:
      pass

print(answer)

Submission status: Wrong Answer

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 = 1000
        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 + 10:
                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: Wrong Answer

Solution code:
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        l.sort()
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[2] - a) <= 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])

            while True:
                if i >= n:
                    break
                if abs(take[1] - b) <= 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while True:
                if i >= n:
                    break
                if abs(take[0] - c) <= 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()

Submission status: Wrong Answer

Solution code:
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[2] - a) <= 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])
            
            while True:
                if i >= n:
                    break
                if abs(take[1] - b) <= 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while True:
                if i >= n:
                    break
                if abs(take[0] - c) <= 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()

Submission status: Wrong Answer

Solution code:
import itertools
N,A,B,C=map(int,input().split())
l=list()
for i in range(N):
    l.append(int(input()))


def solve(x,ans,m):
    global N,A,B,C,l
    c=[10000,-1]
    for i in  range(0,len(l)):
        if(abs(l[i]-x)<=10 and abs(l[i]-x)<=c[0]):
            c[0]=abs(l[i]-x)
            c[1]=i
    
    if(c[1]!=-1):
        tmp=l[c[1]]
        l.pop(c[1])
        return(ans+abs(tmp-x))
    else:
        mins=100000
        minstmp=[0,0]
        for i in range(0,len(l)):
            if(mins<abs(x-l[i])):
                pass
            else:
                minstmp=[i,i]
                mins=abs(x-l[i])-10
        check=False
    
        for i in itertools.combinations(l,2):
            tmp=i[0]+i[1]
            if(abs(x-tmp)<mins):
                mins=abs(x-tmp)
                minstmp=[i[0],i[1]]
                check=True
        if(check and len(l)-1>m):
            ans+=10
            l.append(minstmp[0]+minstmp[1])
            l.remove(minstmp[0])
            l.remove(minstmp[1])
            return solve(x,ans,m)
        else:
            mins=100000
            minstmp=[0,0]
            for i in range(0,len(l)):
                
                if(mins<abs(x-l[i])):
                    pass
                else:
                    minstmp=[i,i]
                    mins=abs(x-l[i])-10
            l.pop(minstmp[0])
            return(ans+mins+10)


totalAns=1000000000
import copy
L=copy.deepcopy(l)

ans=0
ans+=solve(A,0,2)
ans+=solve(B,0,1)
ans+=solve(C,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(A,0,2)
ans+=solve(C,0,1)
ans+=solve(B,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0,2)
ans+=solve(A,0,1)
ans+=solve(C,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0,2)
ans+=solve(C,0,1)
ans+=solve(A,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0,2)
ans+=solve(A,0,1)
ans+=solve(B,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0,2)
ans+=solve(B,0,1)
ans+=solve(A,0,0)
if(ans<totalAns):
    totalAns=ans

print(totalAns)

Submission status: Wrong Answer

Solution code:
#119-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
from itertools import permutations
n,a,b,c=IL()
ll=[]
for i in range(n):
    l=I()
    ll.append(l)
ans=float("INF")
for k in range(3,n+1):
    perms=permutations(range(n),k)
    for perm in perms:
        tans=(k-1)*10-2*10
        for i in range(1,k-1):
            for j in range(i+1,k):
                l=perm[:i]
                m=perm[i:j]
                r=perm[j:]
                vl,ml,rl=0,0,0
                for o in l:
                    vl+=ll[o]
                for o in m:
                    ml+=ll[o]
                for o in r:
                    rl+=ll[o]
                vv=[vl,ml,rl]
                vv.sort()
                tans+=abs(c-vv[0])+abs(b-vv[1])+abs(a-vv[2])
                ans=min(ans,tans)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[0] - c) <= 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            while True:
                if i >= n:
                    break
                if abs(take[1] - b) <= 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while True:
                if i >= n:
                    break
                if abs(take[2] - a) <= 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()

Submission status: Wrong Answer

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

from itertools import combinations, permutations
ans = float('inf')
for i in range(3, N+1):
    for c in combinations(l,i):
        for l1,l2,*li in permutations(c):
            l3 = sum(li)
            for a,b,c in permutations([A,B,C]):
                cost = (len(li)-1)*10
                cost += abs(l1-a) + abs(l2-b) + abs(l3-c)
                ans = min(ans, cost)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


import sys
import math

n, a, b, c = list(map(int, input().split()))

l = []
for i in range(n):
    l.append(int(input()))


##llのリストの時の最小MPを返す
# これは愚直に伸び縮みさせる or 任意の2本をくっつけた時の再起関数の値+10の最小
def saiki(ll: []):
    if (len(ll) == 3):
        ll.sort(reverse=True)
        ret = 0
        ret += abs(ll[0] - a)
        ret += abs(ll[1] - b)
        ret += abs(ll[2] - c)
        #print('len ' + str(ll) + ' min ' + str(ret))
        return ret

    ret = 99999999
    ##愚直なやつ
    llen = len(ll)
    for aa in range(llen - 2):
        for bb in range(aa + 1, llen - 1):
            for cc in range(bb + 1, llen):
                ret = min(saiki([ll[aa], ll[bb], ll[cc]]), ret)

    next = ll.copy()
    ##任意の2つをつなげて再起
    for a1 in range(llen - 1):
        for a2 in range(a1 + 1, llen):
            kuttuke = next[a1] + next[a2]
            del (next[a2])
            del (next[a1])
            next.append(kuttuke)
            ret = min(ret, 10 + saiki(next))
            del (next[llen-2])
            next.append(ll[a1])
            next.append(ll[a2])
    #print('len ' + str(ll) + ' min ' + str(ret))
    return ret


print(saiki(l))

Submission status: Wrong Answer

Solution code:
import numpy as np

n, a, b, c = map(int, input().split(' '))
ls = []
for _ in range(n):
    ls.append(int(input()))
n = 0


def solve(obj):
    c = len(ls)
    dp = np.zeros((obj + 1, c + 1), dtype=int)
    dp[1:, 0] = 10000
    for j in range(1, c + 1):
        dp[0, j] = min(ls[:j])
    for i in range(1, obj + 1):
        for j in range(1, c + 1):
            dp[i, j] = min(dp[i - 1, j] + 1, dp[i, j - 1], abs(ls[j - 1] - i))
            if i >= ls[j - 1]:
                dp[i, j] = min(dp[i, j], dp[i - ls[j - 1], j - 1] + 10)
  
    return dp[obj, c]


for obj in [a, b, c]:
    if obj in ls:
        continue
    else:
        n += solve(obj)
print(n)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
nu = [a,b,c]
ans = [[[0,0],[0,0],[0,0],[0,0]] for i in range(4 ** n)]
lis = [int(input()) for i in range(n)]
def to4(n):
    if int(n//4):
        return to4(n//4) + str(n%4)
    return str(n%4)
for i in range(4 ** n):
    s = to4(i).zfill(n)
    for j in range(n):
        ans[i][int(s[j])][0] += lis[j]
        ans[i][int(s[j])][1] += 1
an = 10 ** 18
for i in range(4 ** n):
    a = 0
    for num in range(3):
        a += abs(ans[i][num][0] -nu[num])
        a += max(0,ans[i][num][1]-1) * 10
    an = min(an,a)
print(an)