# Program to find sum of the minimums of each sublist from a list in Python

Suppose we have a list of numbers called nums. We have to find the sum of minimum of x for every sublist x in nums. If the answer is too large, then mod the result by 10^9 + 7.

So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be 90 because, the sublists are [[5], [10], [20], [10], [0], [5,10], [10,20], [20,10], [10,0], [5,10,20], [10,20,10], [20,10,0], [5,10,20,10], [10,20,10,0], [5,10,20,10,0]], and their minimum values are [5, 10, 20, 10, 0, 5, 10, 10, 0, 5, 10, 0, 5, 0, 0], so the sum is 90.

To solve this, we will follow these steps −

• ans := 0
• s := a new list
• temp_sum := 0
• for each index and value in nums, do
• while s and value <= element at index 1 of last list in s, do
• temp_sum := temp_sum - element at index 2 of last list in s
• delete last element from s
• if s is empty, then
• insert a list with three elements [index, value, (index + 1)*value] in s
• otherwise,
• insert a list with three elements [index, value, (index - first element of the last list of s)*value]
• temp_sum := temp_sum + element at index 2 of last list in s
• ans := ans + temp_sum
• return ans mod (10^9 + 7)

## Example

Let us see the following implementation to get better understanding −

def solve(nums):
ans = 0
s = []
temp_sum = 0
for index, value in enumerate(nums):
while s and value <= s[-1][1]:
temp_sum -= s[-1][2]
s.pop()
if not s:
s.append([index, value, (index + 1) * value])
else:
s.append([index, value, (index - s[-1][0]) * value])
temp_sum += s[-1][2]
ans += temp_sum
return ans % (10**9 + 7)

nums = [5, 10, 20, 10, 0]
print(solve(nums))

## Input

[5, 10, 20, 10, 0]

## Output

90