Given an array **A** having **N** elements and two integers L and R where, and . You can choose any element of the array (let’s say **a _{x}**) and delete it, and also delete all elements equal to

**a**,

_{x}+1**a**…

_{x}+2**a**and

_{x}+R**a**,

_{x}-1**a**…

_{x}-2**a**from the array. This step will cost

_{x}-L**a**points. The task is to maximize the total cost after deleting all the elements from the array.

_{x}**Examples:**

Input : 2 1 2 3 2 2 1 L = 1, R = 1 Output : 8 We select 2 to delete, then (2-1)=1 and (2+1)=3 will need to be deleted, for given L and R range respectively. Repeat this until 2 is completely removed. So, total cost = 2*4 = 8. Input : 2 4 2 9 5 L = 1, R = 2 Output : 18 We select 2 to delete, then 5 and then 9. So total cost = 2*2 + 5 + 9 = 18.

**Approach:** We will find the count of all the elements. Now let’s say an element X is selected then, all elements in the range [X-L, X+R] will be deleted. Now we select the minimum range from L and R and finds up to which elements are to be deleted when element X is selected. Our results will be maximum of previously deleted elements and when element X is deleted. We will use dynamic programming to store the result of previously deleted elements and use it further.

## C++

`// C++ program to find maximum cost after ` `// deleting all the elements form the array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return maximum cost obtained ` `int` `maxCost(` `int` `a[], ` `int` `n, ` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `int` `mx = 0, k; ` ` ` `// find maximum element of the array. ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `mx = max(mx, a[i]); ` ` ` ` ` `// initialize count of all elements to zero. ` ` ` `int` `count[mx + 1]; ` ` ` `memset` `(count, 0, ` `sizeof` `(count)); ` ` ` ` ` `// calculate frequency of all elements of array. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `count[a[i]]++; ` ` ` ` ` `// stores cost of deleted elements. ` ` ` `int` `res[mx + 1]; ` ` ` `res[0] = 0; ` ` ` ` ` `// selecting minimum range from L and R. ` ` ` `l = min(l, r); ` ` ` ` ` `for` `(` `int` `num = 1; num <= mx; num++) { ` ` ` ` ` `// finds upto which elements are to be ` ` ` `// deleted when element num is selected. ` ` ` `k = max(num - l - 1, 0); ` ` ` ` ` `// get maximum when selecting element num or not. ` ` ` `res[num] = max(res[num - 1], num * count[num] + res[k]); ` ` ` `} ` ` ` ` ` `return` `res[mx]; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `a[] = { 2, 1, 2, 3, 2, 2, 1 }, l = 1, r = 1; ` ` ` ` ` `// size of array ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// function call to find maximum cost ` ` ` `cout << maxCost(a, n, l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to find maximum cost after ` `//deleting all the elements form the array ` ` ` `public` `class` `GFG { ` ` ` ` ` `//function to return maximum cost obtained ` ` ` `static` `int` `maxCost(` `int` `a[], ` `int` `n, ` `int` `l, ` `int` `r) ` ` ` `{ ` ` ` ` ` `int` `mx = ` `0` `, k; ` ` ` `// find maximum element of the array. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `mx = Math.max(mx, a[i]); ` ` ` ` ` `// initialize count of all elements to zero. ` ` ` `int` `[] count = ` `new` `int` `[mx + ` `1` `]; ` ` ` `for` `(` `int` `i = ` `0` `; i < count.length; i++) ` ` ` `count[i] = ` `0` `; ` ` ` ` ` `// calculate frequency of all elements of array. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `count[a[i]]++; ` ` ` ` ` `// stores cost of deleted elements. ` ` ` `int` `[] res = ` `new` `int` `[mx + ` `1` `]; ` ` ` `res[` `0` `] = ` `0` `; ` ` ` ` ` `// selecting minimum range from L and R. ` ` ` `l = Math.min(l, r); ` ` ` ` ` `for` `(` `int` `num = ` `1` `; num <= mx; num++) { ` ` ` ` ` `// finds upto which elements are to be ` ` ` `// deleted when element num is selected. ` ` ` `k = Math.max(num - l - ` `1` `, ` `0` `); ` ` ` ` ` `// get maximum when selecting element num or not. ` ` ` `res[num] = Math.max(res[num - ` `1` `], num * count[num] + res[k]); ` ` ` `} ` ` ` ` ` `return` `res[mx]; ` ` ` `} ` ` ` ` ` `//Driver program ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `a[] = { ` `2` `, ` `1` `, ` `2` `, ` `3` `, ` `2` `, ` `2` `, ` `1` `}, l = ` `1` `, r = ` `1` `; ` ` ` ` ` `// size of array ` ` ` `int` `n = a.length; ` ` ` ` ` `// function call to find maximum cost ` ` ` `System.out.println(maxCost(a, n, l, r)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to find maximum cost after ` `# deleting all the elements form the array ` ` ` `# function to return maximum cost obtained ` `def` `maxCost(a, n, l, r) : ` ` ` ` ` `mx ` `=` `0` ` ` ` ` `# find maximum element of the array. ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `mx ` `=` `max` `(mx, a[i]) ` ` ` ` ` `# create and initialize count of all elements to zero. ` ` ` `count ` `=` `[` `0` `] ` `*` `(mx ` `+` `1` `) ` ` ` ` ` `# calculate frequency of all elements of array. ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `count[a[i]] ` `+` `=` `1` ` ` ` ` `# stores cost of deleted elements. ` ` ` `res ` `=` `[` `0` `] ` `*` `(mx ` `+` `1` `) ` ` ` `res[` `0` `] ` `=` `0` ` ` ` ` `# selecting minimum range from L and R. ` ` ` `l ` `=` `min` `(l, r) ` ` ` ` ` `for` `num ` `in` `range` `(` `1` `, mx ` `+` `1` `) : ` ` ` ` ` `# finds upto which elements are to be ` ` ` `# deleted when element num is selected. ` ` ` `k ` `=` `max` `(num ` `-` `l ` `-` `1` `, ` `0` `) ` ` ` ` ` `# get maximum when selecting element num or not. ` ` ` `res[num] ` `=` `max` `(res[num ` `-` `1` `], num ` `*` `count[num] ` `+` `res[k]) ` ` ` ` ` `return` `res[mx] ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[` `2` `, ` `1` `, ` `2` `, ` `3` `, ` `2` `, ` `2` `, ` `1` `] ` ` ` `l, r ` `=` `1` `, ` `1` ` ` ` ` `# size of array ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `# function call to find maximum cost ` ` ` `print` `(maxCost(a, n, l, r)) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum cost ` `// after deleting all the elements ` `// form the array ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to return maximum ` `// cost obtained ` `static` `int` `maxCost(` `int` `[]a, ` `int` `n, ` ` ` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `mx = 0, k; ` ` ` ` ` `// find maximum element ` ` ` `// of the array. ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `mx = Math.Max(mx, a[i]); ` ` ` ` ` `// initialize count of all ` ` ` `// elements to zero. ` ` ` `int` `[] count = ` `new` `int` `[mx + 1]; ` ` ` `for` `(` `int` `i = 0; i < count.Length; i++) ` ` ` `count[i] = 0; ` ` ` ` ` `// calculate frequency of all ` ` ` `// elements of array. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `count[a[i]]++; ` ` ` ` ` `// stores cost of deleted elements. ` ` ` `int` `[] res = ` `new` `int` `[mx + 1]; ` ` ` `res[0] = 0; ` ` ` ` ` `// selecting minimum range ` ` ` `// from L and R. ` ` ` `l = Math.Min(l, r); ` ` ` ` ` `for` `(` `int` `num = 1; num <= mx; num++) ` ` ` `{ ` ` ` ` ` `// finds upto which elements ` ` ` `// are to be deleted when ` ` ` `// element num is selected. ` ` ` `k = Math.Max(num - l - 1, 0); ` ` ` ` ` `// get maximum when selecting ` ` ` `// element num or not. ` ` ` `res[num] = Math.Max(res[num - 1], num * ` ` ` `count[num] + res[k]); ` ` ` `} ` ` ` `return` `res[mx]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]a = { 2, 1, 2, 3, 2, 2, 1 }; ` ` ` `int` `l = 1, r = 1; ` ` ` ` ` `// size of array ` ` ` `int` `n = a.Length; ` ` ` ` ` `// function call to find maximum cost ` ` ` `Console.WriteLine(maxCost(a, n, l, r)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity:** O(max(A))

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:

- Reduce the array by deleting elements which are greater than all elements to its left
- Maximum array sum that can be obtained after exactly k changes
- Maximum possible middle element of the array after deleting exactly k elements
- Check if sum Y can be obtained from the Array by the given operations
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Maximum score of deleting an element from an Array based on given condition
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Change in Median of given array after deleting given elements
- Maximum value obtained by performing given operations in an Array
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Minimum sum obtained from groups of four elements from the given array
- Ways to choose three points with distance between the most distant points <= L
- Steps required to visit M points in order on a circular ring of N points

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.