Given an array **arr[]** of **N** positive integers. We can choose any one index(say **K**) of the array and reduce all the elements of the array from index **0** to **K – 1** by 1. The cost of this operation is **K**. If at any index(say **idx**) element is reduced to **0** then we can’t perform this operation in the range **[idx, N]**. The task is to maximize the cost of reduced operations until we are not able to perform the operation.

**Examples:**

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

Firstly, choose k=5 then remove each element from 0 to 4, and array becomes [2, 1, 0, 3, 4].

Secondly, choose k=2 then remove each element from 0 to 1, and array becomes [1, 0, 0, 3, 4].

Finally, choose k=1, then remove each element from 0 to 0, and array becomes [0, 0, 0, 3, 4].

Hence, the answer is 5+2+1=8

Input:arr[] = {4, 3, 2, 1}Output:10Explanation:

Choose k=4 then 3 then 2 then 1, hence answer becomes 4+3+2+1=10

**Naive Approach:**

- Choose the minimum element from the array in the range
**[0, N]**. - Let the minimum element be at index
**idx**. - Reduce all the elements by 1
**idx**number of times and count this operation. - Now element at index
**idx**is 0. Perform the above operations again by choosing the minimum element in the range**[0, idx]**. - Repeat the above steps until the first element of the array is non-zero.
- The count of operation in the above steps is the maximu17m cost of operation.

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach:** To maximize the cost of the operation always choose the maximum index from the array. While choosing the maximum index(say **idx**) and performing operations, if at any index the element reduces to zero then reduces the choosing index to **idx**. Below are the steps:

- Traverse the given array.
- While traversing the array find the minimum element found so far till every index.
- The sum of the minimum element for each index is the total cost of operations required.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include<bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function that finds the maximum cost` `// of all the operations ` `int` `maxCost(` `int` `arr[], ` `int` `N)` `{` ` ` ` ` `// Initialise maxi with positive` ` ` `// integer value` ` ` `int` `maxi = INT_MAX;` ` ` ` ` `// Initialise the answer variable` ` ` `int` `ans = 0;` ` ` ` ` `// Iterate linearly in the array` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// Find minimum at each step` ` ` `maxi = min(maxi, arr[i]);` ` ` ` ` `// Add maximum to ans` ` ` `ans = ans + maxi;` ` ` `}` ` ` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` ` ` `// Length of the array` ` ` `int` `N = 4;` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = { 4, 3, 2, 1 };` ` ` ` ` `// Function call` ` ` `int` `answer = maxCost(arr, N);` ` ` ` ` `// Print the result` ` ` `cout << (answer);` `}` `// This code is contributed by princiraj1992` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `class` `GFG{` ` ` `// Function that finds the maximum cost` `// of all the operations ` `public` `static` `int` `maxCost(` `int` `arr[], ` `int` `N)` `{` ` ` ` ` `// Initialise maxi with positive` ` ` `// integer value` ` ` `int` `maxi = Integer.MAX_VALUE;` ` ` ` ` `// Initialise the answer variable` ` ` `int` `ans = ` `0` `;` ` ` ` ` `// Iterate linearly in the array` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` ` ` `// Find minimum at each step` ` ` `maxi = Math.min(maxi, arr[i]);` ` ` ` ` `// Add maximum to ans` ` ` `ans = ans + maxi;` ` ` `}` ` ` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Length of the array` ` ` `int` `N = ` `4` `;` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = { ` `4` `, ` `3` `, ` `2` `, ` `1` `};` ` ` ` ` `// Function call` ` ` `int` `answer = maxCost(arr, N);` ` ` ` ` `// Print the result` ` ` `System.out.println(answer);` `}` `}` `// This code is contributed by stutipathak31jan` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function that finds the maximum cost` `# of all the operations` `def` `maxCost(arr, N):` ` ` `# Initialize maxi with positive` ` ` `# infinity value` ` ` `maxi ` `=` `float` `(` `"inf"` `)` ` ` `# Initialise the answer variable` ` ` `ans ` `=` `0` ` ` `# Iterate linearly in the array` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Find minimum at each step` ` ` `maxi ` `=` `min` `(maxi, arr[i])` ` ` `# Add maximum to ans` ` ` `ans ` `=` `ans ` `+` `maxi` `# Return the answer` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `# Length of the array` ` ` `N ` `=` `4` ` ` `# Given array arr[]` ` ` `arr ` `=` `[` `4` `, ` `3` `, ` `2` `, ` `1` `]` ` ` `# Function call` ` ` `answer ` `=` `maxCost(arr, N)` ` ` `# Print the result` ` ` `print` `(answer)` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function that finds the maximum cost` `// of all the operations ` `public` `static` `int` `maxCost(` `int` `[]arr, ` `int` `N)` `{` ` ` ` ` `// Initialise maxi with positive` ` ` `// integer value` ` ` `int` `maxi = ` `int` `.MaxValue;` ` ` ` ` `// Initialise the answer variable` ` ` `int` `ans = 0;` ` ` ` ` `// Iterate linearly in the array` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// Find minimum at each step` ` ` `maxi = Math.Min(maxi, arr[i]);` ` ` ` ` `// Add maximum to ans` ` ` `ans = ans + maxi;` ` ` `}` ` ` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` ` ` `// Length of the array` ` ` `int` `N = 4;` ` ` ` ` `// Given array []arr` ` ` `int` `[]arr = { 4, 3, 2, 1 };` ` ` ` ` `// Function call` ` ` `int` `answer = maxCost(arr, N);` ` ` ` ` `// Print the result` ` ` `Console.WriteLine(answer);` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity:** O(N)**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:

- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Find maximum value of the last element after reducing the array with given operations
- Find last remaining element after reducing the Array
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Maximize sum of K elements in Array by taking only corner elements
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize the median of the given array after adding K elements to the same array
- Maximize Array sum by swapping at most K elements with another array
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Make all array elements equal with minimum cost
- Minimum cost to equal all elements of array using two operation
- Minimum cost of choosing 3 increasing elements in an array of size N
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Minimum cost to make all array elements equal
- Make array elements equal with minimum cost

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.