Minimum Cost to Combine All Elements
Heaps, Sorting & SearchAuto-graded · 22 test casesLast updated
Tests greedy algorithm design and min-heap operations to repeatedly merge pairs optimally.
You are given an array of positive integers. You must reduce the array to a single element by repeatedly performing this operation:
- Select any two elements from the array
- Remove both elements
- Add their sum back into the array
- Pay a cost equal to that sum
Return the minimum total cost required to reduce the array to one element.
If the array contains only one element, no operations are needed and the cost is zero.
Constraints
- 1 ≤ array length ≤ 10⁵
- 1 ≤ each element ≤ 10⁹
- All elements are positive integers
- The operation must be repeated until exactly one element remains
Examples
Example 1
Input: minimum_combination_cost([4, 3, 2, 6])
Output: 29
Example 2
Input: minimum_combination_cost([1, 2, 3, 4, 5])
Output: 33
Example 3
Input: minimum_combination_cost([10])
Output: 0
Example 4
Input: minimum_combination_cost([5, 5, 5, 5])
Output: 40
Hints
Hint 1
What data structure lets you efficiently find and extract the two smallest elements repeatedly?
Hint 2
Think about which pairs you should combine first. Does combining the largest elements early help or hurt?
Hint 3
Consider modeling the merges as a tree where leaves are original numbers and internal nodes are sums. How does the tree structure affect the total cost?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The optimal strategy is to always combine the two smallest available elements. This is the same principle behind Huffman coding trees.
To understand why, imagine the merges forming a binary tree where original numbers are leaves and each internal node represents a combination. Each original number contributes to the total cost once for every merge operation that includes it (either directly or as part of a previously combined sum). This contribution equals the number of times that value is added, which corresponds to its depth in the merge tree.
The total cost can be expressed as the sum of each original value multiplied by its depth. To minimize this weighted sum, larger values should have smaller depths (be combined later) while smaller values can be deeper (combined early). The greedy approach of always merging the two smallest ensures this optimal structure.
Why not just sort once and merge left to right? Because newly created sums must compete with remaining original values. For example, with [1,1,1,1], a linear merge gives cost 9, but the optimal balanced approach gives cost 8.
Algorithm
- Handle the base case: if there's one or zero elements, return 0
- Build a min-heap from all elements in O(n) time
- While more than one element remains:
- Extract the two smallest elements
- Compute their sum
- Add the sum to the running total cost
- Insert the sum back into the heap
- Return the total cost
Complexity
Time: O(n log n)
- Building the heap takes O(n)
- We perform n-1 merge operations
- Each operation does two pops and one push, each O(log n)
- Total: O(n) + O(n log n) = O(n log n)
Space: O(n) for the heap storage
Solution
import heapq
from typing import List
def minimum_combination_cost(nums: List[int]) -> int:
if len(nums) <= 1:
return 0
heap = list(nums)
heapq.heapify(heap)
total_cost = 0
while len(heap) > 1:
first = heapq.heappop(heap)
second = heapq.heappop(heap)
combined = first + second
total_cost += combined
heapq.heappush(heap, combined)
return total_cost