Given an array **arr[]** consisting of **N** integers, the task is to find the cost to remove all array elements after performing the following operations in the specified order any number of times:

- Add the maximum element present in the given array to the cost.
- Remove the maximum element from the given array.
- Decrease all integers in the given array by
**1**. - Remove the elements reduced to 0.

**Examples:**

Input:arr[] = {1, 6, 7, 4, 2, 5, 3}Output:16Explanation:

Step 1: Current maximum element = 7. Removing 7 and reducing each element by 1 modifies the array to {0, 5, 3, 1, 4, 2}. Cost = 7.

Step 2: Current maximum element = 5. Removing 5 and reducing each element by 1 modifies the array to {2, 0, 3, 1}. Cost = 7 + 5 = 12.

Step 3: Current maximum = 3. Removing 3 and reducing each element by 1 modifies the array to {1, 1}. Cost = 12 + 3 = 15

Step 4: Final cost obtained = 15 + 1 = 16

Input:arr[] = {6, 4, 6, 1}Output:13

**Naive Approach:** The simplest approach to solve the problem is as follows:

- Find the maximum from the given array
- Add it to the
**cost**after removing it from the array. - Once the maximum element is removed, reduce all remaining array elements by 1. Remove all elements reducing to 0.
- Repeat the above steps until all array elements are removed.
- Finally, print the value of
**cost**.

**Time Complexity: **O(N^{2}), where N is the size of the given array.**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, the idea is to observe that every i^{th} greatest element in the array is added **arr[i] – i** times to the **cost** where i is the index of each element **arr[i]**. Follow the below steps to solve the above problem:

- Sort the given array in decreasing order.
- Traverse the array and add the value
**arr[i] – i**for each array element to**cost**if it is greater than**0**. - Once the above steps are completed, print the value of
**cost**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the total cost` `// of removing all array elements` `int` `findCost(` `int` `* a, ` `int` `n)` `{` ` ` `// Sort the array in descending order` ` ` `sort(a, a + n, greater<` `int` `>());` ` ` `// Stores the total cost` ` ` `int` `count = 0;` ` ` `for` `(` `int` `j = 0; j < n; j++) {` ` ` `// Contribution of i-th` ` ` `// greatest element to the cost` ` ` `int` `p = a[j] - j;` ` ` `// Remove the element` ` ` `a[j] = 0;` ` ` `// If negative` ` ` `if` `(p < 0) {` ` ` `p = 0;` ` ` `continue` `;` ` ` `}` ` ` `// Add to the final cost` ` ` `count += p;` ` ` `}` ` ` `// Return the cost` ` ` `return` `count;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given array arr[]` ` ` `int` `arr[] = { 1, 6, 7, 4, 2, 5, 3 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `cout << findCost(arr, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the total cost` `// of removing all array elements` `static` `int` `findCost(Integer [] a, ` `int` `n)` `{` ` ` `// Sort the array in descending order` ` ` `Arrays.sort(a, Collections.reverseOrder());` ` ` `// Stores the total cost` ` ` `int` `count = ` `0` `;` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` `{` ` ` `// Contribution of i-th` ` ` `// greatest element to the cost` ` ` `int` `p = a[j] - j;` ` ` `// Remove the element` ` ` `a[j] = ` `0` `;` ` ` `// If negative` ` ` `if` `(p < ` `0` `) ` ` ` `{` ` ` `p = ` `0` `;` ` ` `continue` `;` ` ` `}` ` ` `// Add to the final cost` ` ` `count += p;` ` ` `}` ` ` `// Return the cost` ` ` `return` `count;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given array arr[]` ` ` `Integer arr[] = {` `1` `, ` `6` `, ` `7` `, ` ` ` `4` `, ` `2` `, ` `5` `, ` `3` `};` ` ` `int` `N = arr.length;` ` ` `// Function Call` ` ` `System.out.print(findCost(arr, N));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the total cost` `# of removing all array elements` `def` `findCost(a, n):` ` ` `# Sort the array in descending order` ` ` `a.sort(reverse ` `=` `True` `)` ` ` `# Stores the total cost` ` ` `count ` `=` `0` ` ` `for` `j ` `in` `range` `(n):` ` ` `# Contribution of i-th` ` ` `# greatest element to the cost` ` ` `p ` `=` `a[j] ` `-` `j` ` ` `# Remove the element` ` ` `a[j] ` `=` `0` ` ` `# If negative` ` ` `if` `(p < ` `0` `):` ` ` `p ` `=` `0` ` ` `continue` ` ` `# Add to the final cost` ` ` `count ` `+` `=` `p` ` ` `# Return the cost` ` ` `return` `count` `# Driver Code` `# Given array arr[]` `arr ` `=` `[ ` `1` `, ` `6` `, ` `7` `, ` `4` `, ` `2` `, ` `5` `, ` `3` `]` `N ` `=` `len` `(arr)` `# Function call` `print` `(findCost(arr, N))` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find the total cost` `// of removing all array elements` `static` `int` `findCost(` `int` `[]a, ` `int` `n)` `{` ` ` `// Sort the array in descending order` ` ` `Array.Sort(a);` ` ` `Array.Reverse(a);` ` ` `// Stores the total cost` ` ` `int` `count = 0;` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `{` ` ` `// Contribution of i-th` ` ` `// greatest element to the cost` ` ` `int` `p = a[j] - j;` ` ` `// Remove the element` ` ` `a[j] = 0;` ` ` `// If negative` ` ` `if` `(p < 0) ` ` ` `{` ` ` `p = 0;` ` ` `continue` `;` ` ` `}` ` ` `// Add to the readonly cost` ` ` `count += p;` ` ` `}` ` ` `// Return the cost` ` ` `return` `count;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given array []arr` ` ` `int` `[]arr = {1, 6, 7, ` ` ` `4, 2, 5, 3};` ` ` `int` `N = arr.Length;` ` ` `// Function Call` ` ` `Console.Write(findCost(arr, N));` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity:** O(NlogN), where N is the size of the given array.**Auxiliary Space:** O(1)

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 a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Sum of all possible strings obtained by removal of non-empty substrings
- Maximize cost obtained by removal of substrings "pr" or "rp" from a given String
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Maximum removal from array when removal time >= waiting time
- Minimum removal of subsequences of distinct consecutive characters required to empty a given string
- Minimum removal of consecutive similar characters required to empty a Binary String
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum peak elements from an array by their repeated removal at every iteration of the array
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Maximum value obtained by performing given operations in an Array
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Maximize cost to empty given array by repetitively removing K array elements
- Check if sum Y can be obtained from the Array by the given operations
- Arrange the array such that upon performing given operations an increasing order is obtained
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Minimum cost required to rearrange a given array to make it equal to another given array
- Minimum cost to convert M to N by repeated addition of its even divisors
- Minimize cost to empty a given string by removing characters alphabetically

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.