values. Consider every non-empty segment of it, that is, every run of consecutive elements. Take the smallest element of each segment and add those minimums up.
The sum can be very large, so return it modulo 10⁹ + 7.
Trying every segment takes O(n²); aim for O(n).values=[2, 5, 1] → 12
The segments are [2], [5], [1], [2, 5], [5, 1] and [2, 5, 1]. Their minimums 2, 5, 1, 2, 1, 1 add up to 12.
values=[3, 3] → 9
The segments [3], [3] and [3, 3] each have minimum 3, for a total of 9.
values=[2, 5, 1]
12
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.