Maximum Subarray Min-Product

Given an integer array nums, choose any non-empty subarray. Let the min-product of that subarray be the minimum element multiplied by the sum of the subarray. Return the maximum min-product among all subarrays, modulo 1,000,000,007. Example: Input: nums = [1,2,3,2] Output: 14 Explanation: The best subarray is [2,3,2], whose minimum is 2 and sum is 7, giving 14. Pattern focus: Range Contribution with monotonic stacks and prefix sums.

Input Format

nums = positive integer array

Output Format

maximum min-product of any subarray, modulo 1,000,000,007

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^7

Examples

Example 1:

Input:

nums = [1,2,3,2]

Output:

14

Explanation:

The subarray [2,3,2] maximizes minimum times sum.

Example 2:

Input:

nums = [2,3,3,1,2]

Output:

18

Explanation:

A subarray with a larger sum and a good minimum can be optimal.

Loading...
Maximum Subarray Min-Product