PIXELBANKv9.1.0
Menu

Min Cost to Connect Sticks

You have n sticks of various lengths. Each time you connect two sticks, the cost is the sum of their lengths. Connect all sticks into one with minimum total cost.

Example:

Input:
2,4,3
Output:
14
Reasoning:
  • First, we prioritize connecting the two shortest sticks, which are 2 and 3, resulting in a cost of 2+3=52 + 3 = 5 and a new stick length of 5.
  • Then, we connect the new stick of length 5 with the remaining stick of length 4, resulting in a cost of 5+4=95 + 4 = 9.
  • The total cost is the sum of the costs from each connection: 5+9=145 + 9 = 14.
  • The final output is the total minimum cost to connect all sticks, which is 14.

Constraints:

  • 1 <= len(sticks) <= 10^4
  • 1 <= sticks[i] <= 10^4
solution.py

Test Results

0/0
Run code to see test results.