Optimal solution in Kotlin with explanation
Approach
Keep track of min triplet sum overall, initialized to null
For each midpoint from 1..(size - 2): get min from the left, get min from the right. If it i
# Approach
1. Keep track of min triplet sum overall, initialized to null
2. For each midpoint from `1..(size - 2)`: get min from the left, get min from the right. If it is a valid mountain (`minLeft < this, minRight < this`) – update min triplet overall to min of its old value and current sum, or just set it, if there was no triplet sum value before.
3. Min from the left is caclulated while iterating, we keep around only 1 value. Min from the right – pre-calculate beforehand in a separate loop going from the end to the beginning of the array, storing in a separate array.
Initialize this separate array to be of the same size as the data, even though we don't use the first and the last slots, for ease of calculation.
    ```
    rightMins[i] - min value from > i
    leftMin - min value < i
    ```
4. Return found min sum, or -1 if the variable is still null


# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$ 

# Code
```kotlin []
class Solution {
    fun minimumSum(nums: IntArray): Int {
        var rightMins = IntArray(nums.size)
        rightMins[rightMins.lastIndex - 1] = nums[rightMins.lastIndex]
        for (i in nums.lastIndex - 2 downTo 1) {
            rightMins[i] = minOf(rightMins[i + 1], nums[i + 1])
        }
        
        var leftMin = nums[0]
        var minSum: Int? = null
        for (i in 1 until nums.lastIndex) {
            if (leftMin < nums[i] && rightMins[i] < nums[i]) {
                val currSum = leftMin + nums[i] + rightMins[i]
                minSum = if (minSum == null) currSum else minOf(minSum, currSum)
            }
            
            leftMin = minOf(leftMin, nums[i])
        }
        return minSum ?: -1
    }
}
```
['Kotlin']