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):

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


def dfs(index, a, b, c):
    if N == index:
        if 0 < min(a, b, c):
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return 1 ** 9
    costs = []
    costs.append(dfs(index + 1, a, b, c))
    costs.append(dfs(index + 1, a + l[index], b, c))
    costs.append(dfs(index + 1, a, b + l[index], c))
    costs.append(dfs(index + 1, a, b, c + l[index]))
    return min(costs)


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


ｖn, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
inf = 10 ** 18


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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


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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c)])
    res = min([res, dfs(i + 1, a, b + l[i], c)])
    res = min([res, dfs(i + 1, a, b, c + l[i])])
    return res


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


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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = max([res, dfs(i + 1, a + l[i], b, c)])
    res = max([res, dfs(i + 1, a, b + l[i], c)])
    res = max([res, dfs(i + 1, a, b, c + l[i])])
    return res


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


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


def recur(n, i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(n, i + 1, a, b, c),
        recur(n, i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(n, i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(n, i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(n, 0, 0, 0, 0))


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted