human
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | Function to find maximum sum of difference between maximums and minimums in the splitted subarrays.
Traverse the array.
Stores the maximum and minimum elements upto the i - th index.
Traverse the range [ 0 , i ].
Update the minimum.
Update the maximum.
Update dp [ i ].
Return the maximum sum of difference.
Driver code"
How can the above be solved in Python?
 ... What is a code to split array into subarrays such that sum of difference between their maximums and minimums is maximum and the other steps required?
assistant
#Here is code to split array into subarrays such that sum of difference between their maximums and minimums is maximum and the other steps required.
 def getValue ( arr , N ) :
	dp = [ 0 for i in range ( N ) ]
	for i in range ( 1 , N ) :
		minn = arr [ i ]
		maxx = arr [ i ]
		j = i
		while ( j >= 0 ) :
			minn = min ( arr [ j ] , minn )
			maxx = max ( arr [ j ] , maxx )
			dp [ i ] = max ( dp [ i ] , maxx - minn + ( dp [ j - 1 ] if ( j >= 1 ) else 0 ) )
			j -= 1
	return dp [ N - 1 ]
if __name__ == ' _ _ main _ _ ' :
	arr = [ 8 , 1 , 7 , 9 , 2 ]
	N = len ( arr )
	print ( getValue ( arr , N ) )