Challenge 2: Connecting n pipes with minimum cost

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

Problem Statement

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

Input

An array where its length is the number of pipes and indexes are the specific lengths of the pipes

Output

The total cost of connecting the pipes.

Sample Input

pipes[4] = {4, 3, 2, 6};

Sample Output

29

Create a free account to view this lesson.

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