[JavaScript] Stack w/ Prefix Sum
javascript\nvar maxSumMinProduct = function (nums) {\n    // prefix and postfix nums with a 0 for easy handling\n    nums = [0, ...nums, 0];\n\n    // create a 
```javascript\nvar maxSumMinProduct = function (nums) {\n    // prefix and postfix nums with a 0 for easy handling\n    nums = [0, ...nums, 0];\n\n    // create a prefix sum array so that we can calculate the sum\n    // from i to j in constant time.\n    const prefixSum = [...nums];\n    for (let i = 1; i < nums.length; i++) {\n        prefixSum[i] = prefixSum[i - 1] + prefixSum[i];\n    }\n\n    // stackOfIndices holds indices of ascending values\n    let stackOfIndices = [0];\n    \n    // For every index find the first index to the left that is less \n    // than the value being held at the index i in nums.\n    const leftIndices = new Array(nums.length).fill(0);\n    for (let i = 1; i < nums.length; i++) {\n        // while the current value is less than or equal to the value at the index\n        // being held at the top of the stack, remove that index from the stack\n        // since that means indices greater than or equal to the current index\n        // and indices greater than or equal to the current value will be able to\n        // expand further left to accumulate a larger sum.\n        while (nums[i] <= nums[stackOfIndices[stackOfIndices.length - 1]]) {\n            stackOfIndices.pop();\n        }\n        leftIndices[i] = stackOfIndices[stackOfIndices.length - 1];\n        stackOfIndices.push(i);\n    }\n\n    // clear the stack so that the repeaded measure can be done while iterating to the left\n    // *see above for implementation\n    stackOfIndices = [nums.length];\n    const rightIndices = new Array(nums.length).fill(nums.length - 1);\n    for (let i = nums.length - 2; i > 0; i--) {\n        while (nums[i] <= nums[stackOfIndices[stackOfIndices.length - 1]]) {\n            stackOfIndices.pop();\n        }\n        rightIndices[i] = stackOfIndices[stackOfIndices.length - 1] - 1;\n        stackOfIndices.push(i);\n    }\n\n    let max = BigInt(0);\n    for (let i = 1; i < nums.length - 1; i++) {\n        // with j being the furthest right we can expand while keeping the value at the current index\n        // the smallest value and i being the furthest left we can expand while keeping the value at\n        // the current index the smallest value.\n        // sum from indices [0..j] (inclusive) =  prefixSum[rightIndices[i]]\n        // sum from indices [0...i) (exclusive) = prefixSum[leftIndices[i]]\n        // current value   = nums[i]\n        const sum = BigInt(prefixSum[rightIndices[i]] - prefixSum[leftIndices[i]]);\n        const minProduct = sum * BigInt(nums[i]);\n        if (max < minProduct) {\n            max = minProduct;\n        }\n    }\n    return max % BigInt(1000000007);         //  10^9 + 7\n};\n```
['Stack', 'JavaScript']