Challenge: Connecting n Pipes With Minimum Cost

Given n pipes, find the minimum cost of connecting them.

Problem statement

Implement a function that connects n pipes of different lengths, into one pipe. You can assume that the cost to connect two pipes is equal to the sum of their lengths. We need to connect the pipes with minimum cost.

Input

A list containing lengths of n pipes

Output

The total cost of connecting the pipes

Sample input

pipes = [4, 3, 2, 6]

Sample output

result = 29

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy