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