C++ solution | Using monotonic stack and prefix sum
```\nclass Solution {\nprivate:\n    int mod=1000000000+7;\npublic:\n    int maxSumMinProduct(vector& nums) {\n        //treat each element as the minimum and f
```\nclass Solution {\nprivate:\n    int mod=1000000000+7;\npublic:\n    int maxSumMinProduct(vector<int>& nums) {\n        //treat each element as the minimum and find its left and right bound using monotonic stack\n        //use prefix sum to find subarray sum in constant time\n        int n=nums.size();\n        vector<long long>prefix(n+1,0);\n        for(int i=1;i<prefix.size();i++){\n            prefix[i]=prefix[i-1]+(long long)nums[i-1];\n        }\n        vector<int>left(n,0),right(n,n);\n        vector<int>stack;\n        for(int i=0;i<n;i++){\n            while(stack.size() and nums[stack.back()]>nums[i]){\n                right[stack.back()]=i;\n                stack.pop_back();\n            }\n            stack.push_back(i);\n        }\n        stack.clear();\n        for(int i=n-1;i>=0;i--){\n            while(stack.size() and nums[stack.back()]>nums[i]){\n                left[stack.back()]=i+1;\n                stack.pop_back();\n            }\n            stack.push_back(i);\n        }\n        long long ans=0;\n        for(int i=0;i<n;i++){\n            long long rangeSum=prefix[right[i]]-prefix[left[i]];\n            long long currProduct=(long long)nums[i]*rangeSum;\n            ans=max(ans,currProduct);\n        }\n        return ans%mod;\n    }\n};
['Stack', 'C', 'Monotonic Stack', 'Prefix Sum', 'C++']