Maximum Min-Sum Product

3.3

7 votes
Stack, Advanced Data Structures, Sparse Table, Sparse table, Data Structures
Problem

Given an Array arr of Integers having length N, find the maximum value of min(A[i,j])×sum(A[i,j]) for all 0ij<N where A[i,j] denotes the non-empty subarray [Ai,Ai+1,Ai+2,...,Aj1,Aj].

Since the product may be large, return the product modulo 109+7.

Note:

  •  Testcases are generated such that the maximum product without modulo will fit in a 64-bit signed integer.

Input Format:

  • First line contains an integer T, the number of test cases.
  • First line of each testcase contains an integer N, the length of array.
  • Second line of each testcase contains N space seperated integers, the values of array.

Output Format:

  • For each testcase, output an integer, the maximum value of min(A[i,j])sum(A[i,j]) across all subarrays modulo 109+7.

Constraints:

  • 1T10
  • 105Ai105
  • 1N105
Time Limit: 5.5
Memory Limit: 256
Source Limit:
Explanation
  • In First Test Case, arr=[1,5,1,3,1]arr[2,2]=[5], and min([5])×sum([5])=25
  • In Second Test Case, arr=[0,1,3,1,0], and min(arr)×sum(arr)=3×5=15
  • In Third Test Case, arr=[1,5,1,3,1,3]arr[1,3]=[1,5,1], and min([1,5,1])×sum([1,5,1])=5×7=35
Editor Image

?