File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import sys
2+ import heapq
3+
4+ def main ():
5+ input = sys .stdin .readline
6+
7+ T = int (input ())
8+
9+ for _ in range (T ):
10+ K = int (input ())
11+ fileSize = list (map (int , input ().split ()))[:K ]
12+
13+ heapq .heapify (fileSize )
14+ total = 0
15+
16+ while len (fileSize ) >= 2 :
17+ first = heapq .heappop (fileSize )
18+ second = heapq .heappop (fileSize )
19+ cost = first + second
20+ total += cost
21+
22+ heapq .heappush (fileSize , cost )
23+
24+ print (total )
25+
26+
27+ if __name__ == '__main__' :
28+ main ()
Original file line number Diff line number Diff line change 1+ import sys
2+ import heapq
3+
4+ def main ():
5+ input = sys .stdin .readline
6+
7+ N = int (input ())
8+ cardSize = []
9+
10+ for _ in range (N ):
11+ card = int (input ())
12+ cardSize .append (card )
13+
14+ heapq .heapify (cardSize )
15+ total = 0
16+
17+ while len (cardSize ) >= 2 :
18+ first = heapq .heappop (cardSize )
19+ second = heapq .heappop (cardSize )
20+ cost = first + second
21+ total += cost
22+
23+ heapq .heappush (cardSize , cost )
24+
25+ print (total )
26+
27+ if __name__ == '__main__' :
28+ main ()
You can’t perform that action at this time.
0 commit comments