Sorabh is tasked with fixing a complex system of pipes that is damaged due to wear and tear. The pipes are located in a maze-like underground network, and each pipe has a differing length. In order to fix the system, Sorabh must connect all the pipes into a single, uninterrupted network.
To do this, Sorabh needs to join any two pipes of lengths and into one longer pipe by paying a cost equal to the sum of their lengths . Sorabh must continue joining the pipes until there is only one long pipe left, connecting all the pipes together. Note that after joining two pipes into one, the new pipe is considered to be a pipe with the length .
However, as a responsible plumber, Sorabh wants to keep the costs as low as possible for his client. The lengths of the pipes are given as an array of positive integers .
Can you help Sorabh by calculating the minimum cost required to fix the damaged pipe network?
Hint: To minimize the total cost of fixing the damaged pipe network, you should merge the pipes with the smallest lengths first. Can you use a data structure that always returns the smallest element, so that it can be used to keep track of the smallest pipes? You can use a min-priority queue to keep track of the smallest pipes.