Problem: 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?

Submission attempts (in order):

import copy

def solve(t, l, c):
    
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l

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

    answer = 1000000
    for targets in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
        is_done = [False] * 3
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(len(targets)):
            if targets[i] in L:
                del L[L.index(targets[i])]
                is_done[i] = True

        for i in range(3):
            if is_done[i]:
                continue

            
            # print("try target = {}".format(targets[i]))
            cost, L2 = solve(targets[i], L2, 0)
            # print("cost = {}".format(cost))
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


if __name__ == '__main__':
    main()



import copy

def solve(t, l, c):
    
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l

def main():
    N, A, B, C = map(int, input().split())
    target = [A, B, C]

    is_done = [False] * 3

    L = [int(input()) for _ in range(N)]
    L.sort()

    for i in range(len(target)):
        if target[i] in L:
            del L[L.index(target[i])]
            is_done[i] = True

    answer = 1000000
    for targets in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(3):
            if is_done[i]:
                continue

            
            # print("try target = {}".format(targets[i]))
            cost, L2 = solve(targets[i], L2, 0)
            # print("cost = {}".format(cost))
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


if __name__ == '__main__':
    main()



import itertools
import collections
from bisect import bisect



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

    def solve(i, a, b, c):
        if i == N:
            
            
            return abs(A-a) + abs(B-b) + abs(C-c) - 30 if min(a, b, c) > 0 else int(1e15)
        ret0 = solve(i + 1, a, b, c)
        ret1 = solve(i + 1, a + L[i], b, c) + 10
        ret2 = solve(i + 1, a, b + L[i], c) + 10
        ret3 = solve(i + 1, a, b, c + L[i]) + 10
        return min(ret0, ret1, ret2, ret3)

    print(solve(0, 0, 0, 0))


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted