Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- def min_cost_to_reduce_array(nums):
- heapq.heapify(nums) # Convert the input list into a min-heap
- total_cost = 0
- while len(nums) > 1:
- # Extract the two minimum elements from the heap
- first = heapq.heappop(nums)
- second = heapq.heappop(nums)
- # Calculate the cost of the current move
- cost = first + second
- total_cost += cost
- # Add the sum back to the heap
- heapq.heappush(nums, cost)
- return total_cost
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement