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:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append(l)
    # kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append((kadomatsu_set, 0))
  # kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i][0]) > 3:
  # while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i][0], 2):
    # for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = combination[0] + combination[1]
      # new_kadomastu = (combination[0][0] + combination[1][0], 
      #   combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i][0]]
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      # new_kadomastu_set.remove(combination[0])
      # new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      new_kadomastu_set = []
      continued0 = False
      continued1 = False
      for kadomatsu in kadomatsu_set_list[i][0]:
        # if kadomatsu == combination[0] or kadomatsu == combination[1]: continue
        if not continued0 and kadomatsu == combination[0]:
          continued0 = True
          continue
        if not continued1 and kadomatsu == combination[1]:
          continued1 = True
          continue
        new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append((new_kadomastu_set, kadomatsu_set_list[i][1] + 10))
      # kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  # print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set, set_cost in kadomatsu_set_list:
  # for kadomatsu_set in kadomatsu_set_list:
    do_skip = False
    # print(kadomatsu_set)

    cost = set_cost
    # cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu)
        # diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff
      # cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: # 現時点で最小コストより高い場合はこれ以上探索しない
        # do_skip = True
        break
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    # if do_skip: continue

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append(l)
    # kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append((kadomatsu_set, 0))
  # kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i][0]) > 3:
  # while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i][0], 2):
    # for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = combination[0] + combination[1]
      # new_kadomastu = (combination[0][0] + combination[1][0], 
      #   combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i][0]]
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      # new_kadomastu_set.remove(combination[0])
      # new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      new_kadomastu_set = []
      continued0 = False
      continued1 = False
      for kadomatsu in kadomatsu_set_list[i][0]:
        # if kadomatsu == combination[0] or kadomatsu == combination[1]: continue
        if kadomatsu == combination[0] and not continued0:
          continued0 = True
          continue
        if kadomatsu == combination[1] and not continued1:
          continued1 = True
          continue
        new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append((new_kadomastu_set, kadomatsu_set_list[i][1] + 10))
      # kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  # print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set, set_cost in kadomatsu_set_list:
  # for kadomatsu_set in kadomatsu_set_list:
    do_skip = False
    # print(kadomatsu_set)

    cost = set_cost
    # cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu)
        # diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff
      # cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: # 現時点で最小コストより高い場合はこれ以上探索しない
        # do_skip = True
        break
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    # if do_skip: continue

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append(l)
    # kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append((kadomatsu_set, 0))
  # kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i][0]) > 3:
  # while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i][0], 2):
    # for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = combination[0] + combination[1]
      # new_kadomastu = (combination[0][0] + combination[1][0], 
      #   combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i][0]]
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      # new_kadomastu_set.remove(combination[0])
      # new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      new_kadomastu_set = []
      for kadomatsu in kadomatsu_set_list[i][0]:
        if kadomatsu == combination[0] or kadomatsu == combination[1]: continue
        new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append((new_kadomastu_set, kadomatsu_set_list[i][1] + 10))
      # kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  # print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set, set_cost in kadomatsu_set_list:
  # for kadomatsu_set in kadomatsu_set_list:
    do_skip = False
    # print(kadomatsu_set)

    cost = set_cost
    # cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu)
        # diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff
      # cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: # 現時点で最小コストより高い場合はこれ以上探索しない
        # do_skip = True
        break
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    # if do_skip: continue

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append(l)
    # kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append((kadomatsu_set, 0))
  # kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i][0]) > 3:
  # while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i][0], 2):
    # for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = combination[0] + combination[1]
      # new_kadomastu = (combination[0][0] + combination[1][0], 
      #   combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i][0]]
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      new_kadomastu_set.remove(combination[0])
      new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      # new_kadomastu_set = []
      # for kadomatsu in kadomatsu_set_list[i]:
      #   if kadomatsu is combination[0] or kadomatsu is combination[1]: continue
      #   new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append((new_kadomastu_set, kadomatsu_set_list[i][1] + 10))
      # kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  # print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set, set_cost in kadomatsu_set_list:
  # for kadomatsu_set in kadomatsu_set_list:
    do_skip = False
    # print(kadomatsu_set)

    cost = set_cost
    # cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu)
        # diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff
      # cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: # 現時点で最小コストより高い場合はこれ以上探索しない
        # do_skip = True
        break
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    # if do_skip: continue

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = (combination[0][0] + combination[1][0], 
        combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      new_kadomastu_set.remove(combination[0])
      new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      # new_kadomastu_set = []
      # for kadomatsu in kadomatsu_set_list[i]:
      #   if kadomatsu is combination[0] or kadomatsu is combination[1]: continue
      #   new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  # print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set in kadomatsu_set_list:
    do_skip = False
    # print(kadomatsu_set)

    cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: # 現時点で最小コストより高い場合はこれ以上探索しない
        do_skip = True
        break
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    if do_skip: continue

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:

from itertools import combinations
from copy import deepcopy

def parse():
  N, A, B, C = map(int, input().split(" "))
  L = []
  for n in range(N):
    L.append(int(input()))
  return N, [A, B, C], L

def main():
  N, A, L = parse()
  # print(N, A, B, C, L)

  # 門松セットの初期化
  kadomatsu_set_list = []
  kadomatsu_set = []
  cost_zero = [0, 0, 0]
  for l in L:
    for i, a in enumerate(A):
      if a == l: cost_zero[i] = 1
    kadomatsu_set.append((l, 0))
    # kadomatsu_set.append(Kadomatsu(l, 0))
  kadomatsu_set_list.append(kadomatsu_set)
  # print(kadomatsu_set_list)

  # 初期状態でコスト0の組み合わせがある場合
  if sum(cost_zero) == 3:
    print(0)
    exit(0)

  # kadomatsu_set_list > 3の時，合成可能なので，
  # 合成可能な場合，合成を繰り返す
  i = 0
  while len(kadomatsu_set_list[i]) > 3:
    for combination in combinations(kadomatsu_set_list[i], 2):
      # print("combination:", combination)
      new_kadomastu = (combination[0][0] + combination[1][0], 
        combination[0][1] + combination[1][1] + 10)
      
      # new_kadomastu_setの初期化
      new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]]
      new_kadomastu_set.remove(combination[0])
      new_kadomastu_set.remove(combination[1])
      
      # new_kadomastu_set = [kadomatsu for kadomatsu in kadomatsu_set_list[i]
      #   if (not kadomatsu is combination[0]) and (not kadomatsu is combination[1])]
      
      # new_kadomastu_set = []
      # for kadomatsu in kadomatsu_set_list[i]:
      #   if kadomatsu is combination[0] or kadomatsu is combination[1]: continue
      #   new_kadomastu_set.append(kadomatsu)
      new_kadomastu_set.append(new_kadomastu)
      # print("new_kadomastu_set:", new_kadomastu_set)
      # input()

      kadomatsu_set_list.append(new_kadomastu_set)

    i += 1
  
  print(len(kadomatsu_set_list))
  min_cost = 10 ** 20
  for kadomatsu_set in kadomatsu_set_list:
    # print(kadomatsu_set)

    cost = 0
    for a in A:
      min_diff = 10 ** 10
      min_diff_i = 0
      for i, kadomatsu in enumerate(kadomatsu_set):
        diff = abs(a - kadomatsu[0])
        if min_diff > diff:
          min_diff = diff
          min_diff_i = i
      cost += min_diff + kadomatsu_set[min_diff_i][1]
      if cost > min_cost: break # 現時点で最小コストより高い場合はこれ以上探索しない
      kadomatsu_set.remove(kadomatsu_set[min_diff_i])

    # # Aに最も長さが近い門松を探す
    # min_diff_A = 10 ** 10
    # min_diff_A_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   diff = abs(A - kadomatsu[0])
    #   if min_diff_A > diff:
    #     min_diff_A = diff
    #     min_diff_A_i = i

    # # Bに最も長さが近い門松を探す
    # min_diff_B = 10 ** 10
    # min_diff_B_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   diff = abs(B - kadomatsu[0])
    #   if min_diff_B > diff:
    #     min_diff_B = diff
    #     min_diff_B_i = i
    
    # # Cに最も長さが近い門松を探す
    # min_diff_C = 10 ** 10
    # min_diff_C_i = 0
    # for i, kadomatsu in enumerate(kadomatsu_set):
    #   if i == min_diff_A_i: continue  # Aですでに選んでいる門松はスキップ
    #   if i == min_diff_B_i: continue  # Bですでに選んでいる門松はスキップ
    #   diff = abs(C - kadomatsu[0])
    #   if min_diff_C > diff:
    #     min_diff_C = diff
    #     min_diff_C_i = i
    
    # cost = min_diff_A + min_diff_B + min_diff_C \
      # + kadomatsu_set[min_diff_A_i][1] + kadomatsu_set[min_diff_B_i][1] + kadomatsu_set[min_diff_C_i][1]
      # + kadomatsu_set[min_diff_A_i].cost + kadomatsu_set[min_diff_B_i].cost + kadomatsu_set[min_diff_C_i].cost
    if min_cost > cost: min_cost = cost
    if min_cost == 0: # コスト0の時点で最小値のため即終了
      print(0)
      exit(0)
    # print(min_diff_A_i, min_diff_B_i, min_diff_C_i, cost)
    # input()
  print(min_cost)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


# import numpy as np

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


inf = 1000


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))
# ans = 1000
# for i in itertools.product([0, 1, 2, 3], repeat=n):

#     ans_tmp = 0
#     counter = collections.Counter(i)
#     print(counter)
#     if not((1 in counter) and (2 in counter) and (3 in counter)):
#         continue

#     length = [a, b, c]

#     for index, opt in enumerate(i):
#         if opt != 0:
#             length[opt-1] += L[index]

#     print(length)
#     ans_tmp += (counter[1]-1)*10
#     ans_tmp += (counter[2]-1)*10
#     ans_tmp += (counter[3]-1)*10

#     ans_tmp += abs(a - length[0])
#     ans_tmp += abs(b - length[1])
#     ans_tmp += abs(c - length[2])

#     ans = min(ans, ans_tmp)

# print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  n, a, b, c = map(int, input().split())

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

  def test(i, x, y, z):
    if(i == n):
      if(a == 0 or b == 0 or c == 0):
        return float('inf')
      return abs(a - x) + abs(b - y) + abs(c - z)

    res = test(i + 1, x, y, z)

    res = min(res, test(i + 1, x + l[i], y, z) + (0 if x == 0 else 10)) 
    res = min(res, test(i + 1, x, y + l[i], z) + (0 if y == 0 else 10)) 
    res = min(res, test(i + 1, x, y, z + l[i]) + (0 if z == 0 else 10)) 

    return res

  print(test(0, 0, 0, 0))

if __name__ == '__main__':
  main()