<doocs>2355.Maximum Number of Books You Can Take_1.py
def maximumBooks( books: List[int]) -> int:
    nums = [v - i for i, v in enumerate(books)]
    n = len(nums)
    left = [-1] * n
    stk = []
    for i, v in enumerate(nums):
        while stk and nums[stk[-1]] >= v:
            stk.pop()
        if stk:
            left[i] = stk[-1]
        stk.append(i)
    ans = 0
    dp = [0] * n
    dp[0] = books[0]
    for i, v in enumerate(books):
        j = left[i]
        cnt = min(v, i - j)
        u = v - cnt + 1
        s = (u + v) * cnt // 2
        dp[i] = s + (0 if j == -1 else dp[j])
        ans = max(ans, dp[i])
    return ans
def sample_funcname ( var_0 : List [ int ] ) -> int :
    var_6 = [ var_3 - var_4 for var_4 , var_3 in enumerate ( var_0 ) ]
    var_8 = len ( var_6 )
    var_10 = [ - 1 ] * var_8
    var_11 = [ ]
    for var_4 , var_3 in enumerate ( var_6 ) :
        while var_11 and var_6 [ var_11 [ - 1 ] ] >= var_3 :
            var_11.pop ( )
        if var_11 :
            var_10 [ var_4 ] = var_11 [ - 1 ]
        var_11.append ( var_4 )
    var_15 = 0
    var_16 = [ 0 ] * var_8
    var_16 [ 0 ] = var_0 [ 0 ]
    for var_4 , var_3 in enumerate ( var_0 ) :
        var_17 = var_10 [ var_4 ]
        var_19 = min ( var_3 , var_4 - var_17 )
        var_20 = var_3 - var_19 + 1
        var_22 = ( var_20 + var_3 ) * var_19 // 2
        var_16 [ var_4 ] = var_22 + ( 0 if var_17 == - 1 else var_16 [ var_17 ] )
        var_15 = max ( var_15 , var_16 [ var_4 ] )
    return var_15
