8 line intuitive Python 3 solution using bisect [beat 100% ]
\n        generate all combinations for toppingCosts which has length of 3^m\n        and use linear search or sort and use bisect to find the closest\n\t\t\n\t
\n        generate all combinations for toppingCosts which has length of 3^m\n        and use linear search or sort and use bisect to find the closest\n\t\t\n\t\trun time is n*log(3^m) for bisecting + 3^m*m for sorting = m*(n+3^m)\n\n```\n    def closestCost(self, bc: List[int], tc: List[int], target: int) -> int:\n        combis = [0]\n        for t in tc: combis = [c+i*t for c in combis for i in range(3)]\n        combis, ans = sorted(set(combis)), math.inf\n        for b in bc:\n            idx = bisect.bisect(combis,target-b)\n            if idx < len(combis): ans = min(ans,b+combis[idx], key = lambda x: (abs(target-x),x))\n            if idx > 0:           ans = min(ans,b+combis[idx-1], key = lambda x: (abs(target-x),x))\n        \n        return ans\n```
[]