Skip to content

Commit

Permalink
Add Add-all solution
Browse files Browse the repository at this point in the history
  • Loading branch information
thorvn committed Oct 7, 2022
1 parent 5ec8b69 commit e379b5b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Lecture-07-heap/add-all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import heapq

n = int(input())

while ( n!=0 ):
h = list(map(int, input().split()))
heapq.heapify(h)

cost = 0
while len(h) > 1:
sum = heapq.heappop(h) + heapq.heappop(h)
heapq.heappush(h, sum)
cost += sum

print(cost)

n = int(input())

0 comments on commit e379b5b

Please sign in to comment.