Challenge: Connecting n Pipes with Minimum Cost

Solve the challenge of finding the minimum cost to connect n pipes.

Problem statement

Implement a function that connects nn pipes of different lengths into one pipe. Assume that the cost to connect two pipes is equal to the sum of their lengths. We need to connect the pipes at a minimum cost.

Input

An array containing lengths of nn 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