human
Maximum number of candies that can be bought | Function to return the maximum candies that can be bought.
Buy all the candies of the last type.
Starting from second last.
Amount of candies of the current type that can be bought.
Add candies of current type that can be bought.
Update the previous bought amount.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum number of candies that can be bought and the other steps required.
 def maxCandies ( arr , n ) :
	prevBought = arr [ n - 1 ] ;
	candies = prevBought ;
	for i in range ( n - 2 , - 1 , - 1 ) :
		x = min ( prevBought - 1 , arr [ i ] ) ;
		if ( x >= 0 ) :
			candies += x ;
			prevBought = x ;
	return candies ;
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 1 , 3 , 6 ] ;
	n = len ( arr )
	print ( maxCandies ( arr , n ) ) ;