Dsa
Greedy
Hard
Minimum Cost to Connect Ropes
You have a number of ropes of different lengths that you want to connect into one rope. The cost of connecting two ropes is equal to the sum of their lengths. Write a program that reads the list of rope lengths from STDIN and prints the minimum cost of connecting them all into a single rope to STDOUT.
Input format: An integer n (1 <= n <= 1000) followed by n integers (1 <= lengths[i] <= 10^4) representing the lengths of the ropes.
Output format: A single integer, which is the minimum cost of connecting all ropes.
Example:
Input:
4
4 3 2 6
Output:
29
Key concepts
greedypriority queue
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now