comefrom: <doocs>2355.Maximum Number of Books You Can Take_1.py
code: 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
masked: def sample_funcname ( var_0 : List [ int ] ) -> int :
      var_3 = [ var_1 - var_2 for var_2 , var_1 in enumerate ( var_0 ) ]
      var_4 = len ( var_3 )
      var_5 = [ - 1 ] * var_4
      var_6 = [ ]
      for var_2 , var_1 in enumerate ( var_3 ) :
          while var_6 and var_3 [ var_6 [ - 1 ] ] >= var_1 :
              var_6.pop ( )
          if var_6 :
              var_5 [ var_2 ] = var_6 [ - 1 ]
          var_6.append ( var_2 )
      var_7 = 0
      var_8 = [ 0 ] * var_4
      var_8 [ 0 ] = var_0 [ 0 ]
      for var_2 , var_1 in enumerate ( var_0 ) :
          var_9 = var_5 [ var_2 ]
          var_10 = min ( var_1 , var_2 - var_9 )
          var_11 = var_1 - var_10 + 1
          var_12 = ( var_11 + var_1 ) * var_10 // 2
          var_8 [ var_2 ] = var_12 + ( 0 if var_9 == - 1 else var_8 [ var_9 ] )
          var_7 = max ( var_7 , var_8 [ var_2 ] )
      return var_7
  