func minimumSum(nums []int) int { n := len(nums) minArr := make([]int, n) minArr[0] = nums[0] for i := 1; i < n; i++ { minArr[i] = min(minArr[i-1], nums[i-1]) } pre := nums[n-1] ans := math.MaxInt for i := n - 2; i > 0; i-- { if nums[i] > pre && nums[i] > minArr[i] { ans = min(ans, pre+nums[i]+minArr[i]) } pre = min(pre, nums[i]) } if ans == math.MaxInt { return -1 } return ans }