<doocs>2104.Sum of Subarray Ranges_1.py
def f(nums):
    stk = []
    n = len(nums)
    left = [-1] * n
    right = [n] * n
    for i, v in enumerate(nums):
        while stk and nums[stk[-1]] <= v:
            stk.pop()
        if stk:
            left[i] = stk[-1]
        stk.append(i)
    stk = []
    for i in range(n - 1, -1, -1):
        while stk and nums[stk[-1]] < nums[i]:
            stk.pop()
        if stk:
            right[i] = stk[-1]
        stk.append(i)
    return sum((i - left[i]) * (right[i] - i) * v for i, v in enumerate(nums))
def sample_funcname ( var_0 ) :
    var_1 = [ ]
    var_3 = len ( var_0 )
    var_5 = [ - 1 ] * var_3
    var_6 = [ var_3 ] * var_3
    for var_8 , var_9 in enumerate ( var_0 ) :
        while var_1 and var_0 [ var_1 [ - 1 ] ] <= var_9 :
            var_1.pop ( )
        if var_1 :
            var_5 [ var_8 ] = var_1 [ - 1 ]
        var_1.append ( var_8 )
    var_1 = [ ]
    for var_8 in range ( var_3 - 1 , - 1 , - 1 ) :
        while var_1 and var_0 [ var_1 [ - 1 ] ] < var_0 [ var_8 ] :
            var_1.pop ( )
        if var_1 :
            var_6 [ var_8 ] = var_1 [ - 1 ]
        var_1.append ( var_8 )
    return sum ( ( var_8 - var_5 [ var_8 ] ) * ( var_6 [ var_8 ] - var_8 ) * var_9 for var_8 , var_9 in enumerate ( var_0 ) )
