解题思路:
注意事项:
参考代码:
import heapq
def mergeFruits(fruits):
heap = []
for fruit in fruits:
heapq.heappush(heap, fruit)
total_cost = 0
while len(heap) > 1:
# 从堆中取出两堆果子
fruit1 = heapq.heappop(heap)
fruit2 = heapq.heappop(heap)
# 合并果子堆,并计算合并的体力消耗
merged_fruit = fruit1 + fruit2
total_cost += merged_fruit
# 将合并后的果子堆放回堆中
heapq.heappush(heap, merged_fruit)
return total_cost
# 读取输入
n = int(input())
fruits = list(map(int, input().split()))
# 调用函数计算最小体力耗费值
min_cost = mergeFruits(fruits)
# 输出结果
print(min_cost)
0.0分
0 人评分