# Minimize Cost with Replacement with other allowed

Given an array of n integers, we can remove one element of the array by the following operation.
Operation:We select any two numbers of the array and remove the larger number, Cost including in this operation is equal to the smaller number.
You have to reduce the array into a single element by performing above operations with minimum cost.

Examples:

Input : arr[] = {3 4}
Output : 3
We remove 4 by picking both elements and paying cost equal to smaller.

Input : 4 2 5
Output : 4
We first pick 4, 2, remove 4 by paying cost 2. Then we remove 5 by again paying cost 2.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

As we have to reduce the array to the single element, and it is given that if we select any two numbers, then cost of removing the larger is equal to the smaller number. So to minimize the total cost, we always take the smallest number with other numbers to remove that.so total cost will be (n-1)*smallest number.

 `// C++ Program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// this function returns the minimum ` `// cost of the array ` `int` `getMinCost(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `min_ele = *min_element(arr, arr+n); ` `    ``return` `min_ele * (n - 1); ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 2, 5 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` `    ``cout << getMinCost(arr, n) << endl; ` `    ``return` `0; ` `} `

 `// Java Program for the above approach ` `import` `java.util.Collections; ` `import` `java.util.Arrays; ` ` `  `public` `class` `GfG { ` `     `  `    ``// This function returns the minimum  ` `    ``// cost of the array  ` `    ``public` `static` `int` `getMinCost(Integer arr[], ``int` `n)  ` `    ``{  ` `        ``int` `min_ele = Collections.min(Arrays.asList(arr)); ` `        ``return` `min_ele * (n - ``1``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String []args){ ` `         `  `        ``Integer[] arr = { ``4``, ``2``, ``5` `}; ` `        ``int` `n = arr.length; ` `         `  `        ``System.out.println(getMinCost(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rituraj Jain `

 `# Python3 Program for the above approach ` ` `  `# Function returns the minimum  ` `# cost of the array  ` `def` `getMinCost(arr, n): ` `    ``min_ele ``=` `min``(arr) ` `    ``return` `min_ele ``*` `(n ``-` `1``) ` ` `  `# Driver Code ` `arr ``=` `[``4``, ``2``, ``5``] ` `n ``=` `len``(arr) ` `print``(getMinCost(arr, n)) ` ` `  `# This code is contributed  ` `# by Shrikant13 `

 `// C# Program for the above approach ` `using` `System; ` `using` `System.Linq; ` ` `  `class` `GfG  ` `{ ` `     `  `    ``// This function returns the minimum  ` `    ``// cost of the array  ` `    ``public` `static` `int` `getMinCost(``int` `[]arr, ``int` `n)  ` `    ``{  ` `        ``int` `min_ele = arr.Min(); ` `        ``return` `min_ele * (n - 1);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` `        ``int``[] arr = { 4, 2, 5 }; ` `        ``int` `n = arr.Length; ` `         `  `        ``Console.WriteLine(getMinCost(arr, n)); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

 ` `

Output:
```4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :