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):
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 100 90 80\n"
     ]
    }
   ],
   "source": [
    "N,A,B,C = map(int, input().split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98\n",
      "40\n",
      "30\n",
      "21\n",
      "80\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "for i in range(n):\n",
    "    l.append(int(input()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "INF = 10 ** 9\n",
    "def dfs(cur, a, b, c):\n",
    "    if cur == n:\n",
    "        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a,b,c) > 0 else INF\n",
    "    ret0 = dfs(cur + 1, a, b, c)\n",
    "    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10\n",
    "    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10\n",
    "    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10\n",
    "    return min(ret0, ret1, ret2, ret3)\n",
    "\n",
    "print(dfs(0, 0, 0, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}


N, A, B, C = map(int, input().split())
L = []
for i in range(N):
    L.append(int(input()))
    
import itertools
m = 4
bit_list = list(itertools.product([i for i in range(m)], repeat=N))
ans = 10 ** 18
for bit in bit_list:
    retA = 0
    retB = 0
    retC = 0
    checkA = False
    checkB = False
    checkC = False
    cost = 0
    for i,j in enumerate(bit):
        if j == 1:
            retA += L[i]
            if checkA:
                cost += 10
            checkA = True
        if j == 2:
            retB += L[i]
            if checkB:
                cost += 10
            checkB = True
        if j == 3:
            retC += L[i]
            if checkC:
                cost += 10
            checkC = True
    if min(retA,retB,retC) != 0:
        ans = min(ans, abs(A-retA)+abs(B-retB)+abs(C-retC)+cost)
print(ans) 

Verdicts: Runtime Error

Accepted