Related Articles
Maximum sum possible by making given array non-decreasing
• Last Updated : 25 Nov, 2020

Given an array arr[], the task is to obtain a non-decreasing array with the maximum sum from the given array by repeatedly decrementing array elements by 1.

Explanation:

Input: arr[] = {1, 5, 2, 3, 4}
Output: 12
Explanation: Modify the given array to {1, 2, 2, 3, 4} by reducing 5 to 2 to obtain maximum sum possible from a non-decreasing array.

Input: arr[] = {1, 2, 5, 9, -3}
Output: -15

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the``// above approach``#include ``using` `namespace` `std;``int` `maximumSum(vector<``int``> a,``               ``int` `n)``{``  ``//Traverse the array in``  ``// reverse``  ``for` `(``int` `i = n - 1; i >= 0; i--)``  ``{``    ``//If a[i] is decreasing``    ``if` `(!(a[i - 1] <= a[i]))``      ``a[i - 1] = a[i];``  ``}` `  ``int` `sum = 0;` `  ``for``(``int` `i : a) sum += i;` `  ``//Return sum of the array``  ``return` `sum;``}` `//Driver code``int` `main()``{``  ``//Given array arr[]``  ``vector<``int``> arr = {1, 5, 2, 3, 4};``  ``int` `N = arr.size();` `  ``cout << (maximumSum(arr, N));``}` `// This code is contributed by Mohit Kumar 29`

## Java

 `// Java program for the``// above approach``import` `java.util.*;``class` `GFG{``static` `int` `maximumSum(``int``[] a,``                      ``int` `n)``{``  ``//Traverse the array in``  ``// reverse``  ``for` `(``int` `i = n - ``1``; i > ``0``; i--)``  ``{``    ``//If a[i] is decreasing``    ``if` `(!(a[i - ``1``] <= a[i]))``      ``a[i - ``1``] = a[i];``  ``}` `  ``int` `sum = ``0``;` `  ``for``(``int` `i : a) sum += i;` `  ``//Return sum of the array``  ``return` `sum;``}` `//Driver code``public` `static` `void` `main(String[] args)``{``  ``//Given array arr[]``  ``int``[] arr = {``1``, ``5``, ``2``, ``3``, ``4``};``  ` `  ``int` `N = arr.length;``  ``System.out.print(maximumSum(arr, N));``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 program for the``# above approach` `def` `maximumSum(a, n):``    ` `    ``# Traverse the array in reverse``    ``for` `i ``in` `range``(n``-``1``, ``0``, ``-``1``):``        ` `        ``# If a[i] is decreasing``        ``if` `not` `a[i``-``1``] <``=` `a[i]:``            ``a[i``-``1``] ``=` `a[i]``              ` `    ``# Return sum of the array``    ``return` `sum``(a)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[``1``, ``5``, ``2``, ``3``, ``4``]``    ``N ``=` `len``(arr)``    ` `    ``print``(maximumSum(arr, N))`

## C#

 `// C# program for the``// above approach``using` `System;` `class` `GFG{``    ` `static` `int` `maximumSum(``int``[] a, ``int` `n)``{``    ` `    ``// Traverse the array in``    ``// reverse``    ``for``(``int` `i = n - 1; i > 0; i--)``    ``{``        ` `        ``// If a[i] is decreasing``        ``if` `(!(a[i - 1] <= a[i]))``            ``a[i - 1] = a[i];``    ``}` `    ``int` `sum = 0;` `    ``foreach``(``int` `i ``in` `a) sum += i;` `    ``// Return sum of the array``    ``return` `sum;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []arr``    ``int``[] arr = { 1, 5, 2, 3, 4 };` `    ``int` `N = arr.Length;``    ` `    ``Console.Write(maximumSum(arr, N));``}``}` `// This code is contributed by shikhasingrajput`
Output:
`12`

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.

My Personal Notes arrow_drop_up