c++ Time: O(N) Space: O(N)
The basic idea to solve this question is to calculate product for every element present in array.\nNow our task should be to get sum of valid subarray for curre
The basic idea to solve this question is to calculate product for every element present in array.\nNow our task should be to get sum of valid subarray for current element\nfor exam [3,1,5,6,4,2] in this array, the valid subarray for ele 4 is 5,6,4.\nIf you don\'t want to get TLE then to calculate this sum time complexity should be O(1)\nor O(log(N)).\n\nNow our only task is to calculate sum of valid subarray for every element and store is somewhere so that we can get it in O(1).\n\n```\nclass Solution {\npublic:\n    int mod=1e9+7;\n    \n    int maxSumMinProduct(vector<int>& nums) {\n        unordered_map<int,long long int>total_left,total_right; \n\t\t\t\t//to store sum of every element\n\t\t\t\t//total_left contain sum of left side of ele\n\t\t\t\t//total_right contain sum of right side of ele\n\t\t\t\t\n        stack<int>st;//to get sum of left elements\n        \n        for(int i=0;i<nums.size();i++){\n            if(st.size() == 0){\n                st.push(i);\n                total_left[i]=0;\n            }\n            else if(nums[i] > nums[st.top()]){\n                st.push(i);\n                total_left[i]=0;\n            }\n            else{\n                long long int sum=0;\n                while(st.size() && nums[i] <= nums[st.top()]){\n                    sum+=total_left[st.top()]+nums[st.top()];\n                    st.pop();\n                }\n                total_left[i]=sum;\n                st.push(i);\n            }\n        }\n        \n        stack<int>st1; //to get sum of right elements\n        for(int i=nums.size()-1;i>=0;i--){\n            if(st1.size() == 0){\n                total_right[i]=0;\n                st1.push(i);\n            }\n            else if(nums[i] > nums[st1.top()]){\n                total_right[i]=0;\n                st1.push(i);\n            }\n            else{\n                long long int sum=0;\n                while(st1.size() && nums[i] <= nums[st1.top()]){\n                    sum+=total_right[st1.top()]+nums[st1.top()];\n                    st1.pop();\n                }\n                total_right[i]=sum;\n                st1.push(i);\n            }\n        }\n        \n        long long int res=0,tmp;\n        for(int i=0;i<nums.size();i++){\n            long long int sum=nums[i] + total_left[i] + total_right[i];\n            //total sum of valid subarray\n            tmp=sum * (long long int)nums[i];\n            res=max(tmp,res);\n        }\n        \n        return res % mod;\n    }\n};\n```\n\nIf you want to get good understand of the code ,i\'ll recommend, dry run with any example.
[]