Given **N** elements, you can remove any two elements from the list, note their sum and add the sum to the list. Repeat these steps while there are more than a single element in the list. The task is to minimize the sum of these chosen sum in the end.

**Examples:**

Input:arr[] = {1, 4, 7, 10}Output:39

Choose 1 and 4, Sum = 5, arr[] = {5, 7, 10}

Choose 5 and 7, Sum = 17, arr[] = {12, 10}

Choose 12 and 10,Sum = 39, arr[] = {22}

Input:arr[] = {1, 3, 7, 5, 6}Output:48

**Approach:** In order to minimize the sum, the elements that gets chosen at every step must the minimum elements from the list. In order to do that efficiently, a priority queue can be used. At every step, while there are more than a single element in the list, choose the minimum and the second minimum, remove them from the list add their sum to the list after updating the running sum.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to return the minimized sum ` `int` `getMinSum(` `int` `arr[], ` `int` `n) `
`{ ` ` ` `int` `i, sum = 0; `
` ` ` ` `// Priority queue to store the elements of the array `
` ` `// and retrieve the minimum element efficiently `
` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > pq; `
` ` ` ` `// Add all the elements `
` ` `// to the priority queue `
` ` `for` `(i = 0; i < n; i++) `
` ` `pq.push(arr[i]); `
` ` ` ` `// While there are more than 1 elements `
` ` `// left in the queue `
` ` `while` `(pq.size() > 1) `
` ` `{ `
` ` ` ` `// Remove and get the minimum `
` ` `// element from the queue `
` ` `int` `min = pq.top(); `
` ` ` ` `pq.pop(); `
` ` ` ` `// Remove and get the second minimum `
` ` `// element (currently minimum) `
` ` `int` `secondMin = pq.top(); `
` ` ` ` `pq.pop(); `
` ` ` ` `// Update the sum `
` ` `sum += (min + secondMin); `
` ` ` ` `// Add the sum of the minimum `
` ` `// elements to the queue `
` ` `pq.push(min + secondMin); `
` ` `} `
` ` ` ` `// Return the minimized sum `
` ` `return` `sum; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` ` ` `int` `arr[] = { 1, 3, 7, 5, 6 }; `
` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); `
` ` `cout << (getMinSum(arr, n)); `
`} ` ` ` `// This code is contributed by mohit ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `import` `java.util.PriorityQueue; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// Function to return the minimized sum `
` ` `static` `int` `getMinSum(` `int` `arr[], ` `int` `n) `
` ` `{ `
` ` `int` `i, sum = ` `0` `; `
` ` ` ` `// Priority queue to store the elements of the array `
` ` `// and retrieve the minimum element efficiently `
` ` `PriorityQueue<Integer> pq = ` `new` `PriorityQueue<>(); `
` ` ` ` `// Add all the elements `
` ` `// to the prioriry queue `
` ` `for` `(i = ` `0` `; i < n; i++) `
` ` `pq.add(arr[i]); `
` ` ` ` `// While there are more than 1 elements `
` ` `// left in the queue `
` ` `while` `(pq.size() > ` `1` `) `
` ` `{ `
` ` ` ` `// Remove and get the minimum `
` ` `// element from the queue `
` ` `int` `min = pq.poll(); `
` ` ` ` `// Remove and get the second minimum `
` ` `// element (currently minimum) `
` ` `int` `secondMin = pq.poll(); `
` ` ` ` `// Update the sum `
` ` `sum += (min + secondMin); `
` ` ` ` `// Add the sum of the minimum `
` ` `// elements to the queue `
` ` `pq.add(min + secondMin); `
` ` `} `
` ` ` ` `// Return the minimized sum `
` ` `return` `sum; `
` ` `} `
` ` ` ` `// Driver code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `int` `arr[] = { ` `1` `, ` `3` `, ` `7` `, ` `5` `, ` `6` `}; `
` ` `int` `n = arr.length; `
` ` `System.out.print(getMinSum(arr, n)); `
` ` `} `
`} ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` ` ` `# Function to return the minimized sum ` `def` `getMinSum(arr, n): `
` ` ` ` `sum` `=` `0`
` ` ` ` `# Priority queue to store the elements of the array `
` ` `# and retrieve the minimum element efficiently `
` ` `pq ` `=` `[] `
` ` ` ` `# Add all the elements `
` ` `# to the priority queue `
` ` `for` `i ` `in` `range` `( n ): `
` ` `pq.append(arr[i]) `
` ` ` ` `# While there are more than 1 elements `
` ` `# left in the queue `
` ` `while` `(` `len` `(pq) > ` `1` `) : `
` ` ` ` `pq.sort(reverse` `=` `True` `) `
` ` ` ` `# Remove and get the minimum `
` ` `# element from the queue `
` ` `min` `=` `pq[` `-` `1` `]; `
` ` ` ` `pq.pop(); `
` ` ` ` `# Remove and get the second minimum `
` ` `# element (currently minimum) `
` ` `secondMin ` `=` `pq[` `-` `1` `]; `
` ` ` ` `pq.pop(); `
` ` ` ` `# Update the sum `
` ` `sum` `+` `=` `(` `min` `+` `secondMin); `
` ` ` ` `# Add the sum of the minimum `
` ` `# elements to the queue `
` ` `pq.append(` `min` `+` `secondMin) `
` ` ` ` `# Return the minimized sum `
` ` `return` `sum`
` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `arr ` `=` `[ ` `1` `, ` `3` `, ` `7` `, ` `5` `, ` `6` `] `
` ` `n ` `=` `len` `(arr) `
` ` `print` `(getMinSum(arr, n)) `
` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

48

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.

## Recommended Posts:

- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Reduce array to a single element by repeatedly removing an element from any increasing pair
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Minimize remaining array element by removing pairs and replacing them by their absolute difference
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- Largest number in given Array formed by repeatedly combining two same elements
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Array obtained by repeatedly reversing array after every insertion from given array
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Make sum of all subarrays of length K equal by only inserting elements
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Python | Inserting item in sorted list maintaining order

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 :**