1167. Minimum Cost to Connect Sticks

Problem

Find the minimum cost to connect all sticks into one, where connecting two sticks of lengths x and y costs x+y.

Given an array of stick lengths sticks, you want to connect all sticks into one. Each time you connect two sticks, the cost is equal to the sum of their lengths. You can connect any two sticks repeatedly until only one stick remains. Return the minimum total cost to connect all sticks.

This is equivalent to the classic Huffman coding problem - always connect the two shortest sticks first.

Examples
Input: [2,4,3]
Output: 14
Hints
Related Problems

1167. Minimum Cost to Connect Sticks

Find the minimum cost to connect all sticks into one, where connecting two sticks of lengths x and y costs x+y.