human
Sum of minimum elements of all subarrays | Function to return required minimum sum.
getting number of element strictly larger than A [ i ] on Left ..
get elements from stack until element greater than A [ i ] found.
getting number of element larger than A [ i ] on Right ..
get elements from stack until element greater or equal to A [ i ] found.
calculating required resultult.
Driver Code.
function call to get required resultult"
Solution in Python:
 Give me a code to sum of minimum elements of all subarrays and the other steps required?
assistant
#Here is code to sum of minimum elements of all subarrays and the other steps required.
 def sumSubarrayMins ( A , n ) :
	left , right = [ None ] * n , [ None ] * n
	s1 , s2 = [ ] , [ ]
	for i in range ( 0 , n ) :
		cnt = 1
		while len ( s1 ) > 0 and s1 [ - 1 ] [ 0 ] > A [ i ] :
			cnt += s1 [ - 1 ] [ 1 ]
			s1 . pop ( )
		s1 . append ( [ A [ i ] , cnt ] )
		left [ i ] = cnt
	for i in range ( n - 1 , - 1 , - 1 ) :
		cnt = 1
		while len ( s2 ) > 0 and s2 [ - 1 ] [ 0 ] >= A [ i ] :
			cnt += s2 [ - 1 ] [ 1 ]
			s2 . pop ( )
		s2 . append ( [ A [ i ] , cnt ] )
		right [ i ] = cnt
	result = 0
	for i in range ( 0 , n ) :
		result += A [ i ] * left [ i ] * right [ i ]
	return result
if __name__ == " _ _ main _ _ " :
	A = [ 3 , 1 , 2 , 4 ]
	n = len ( A )
	print ( sumSubarrayMins ( A , n ) )