Connect N Ropes With Minimum Cost
You have been given 'N' ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to connect the ropes with minimum cost.
Input format :
The first line of input contains an integer value 'N'. It denotes the total number of ropes.
The second line of input contains 'N' single space separated integers representing length of each rope i.e. a1, a2, a3, ... an.
Output Format :
The only line of output will contain an integer, the minimum cost for connecting all the ropes.
Note:
You need not to print anything, it has been already taken care of. Just implement the given function.
Constraints :
1 <= N <= 10^5
1 <= ai <= 10^9
Time Limit : 1 sec
Be the first one to answer
Add answer anonymously...
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app