All problems

Problem 904

Maximum Score of a Good Subarray

Hard · 30 pts · Array, Two Pointers, Monotonic Stack

Problem

The score of a block of consecutive elements from i to j is min(NUMS[i..j]) × (j - i + 1). A block is good if it contains index K (i <= K <= j). Find the maximum score of a good block. Start with the block [K, K] and grow it one step at a time, always extending towards the side whose next element is larger, so the minimum drops as slowly as possible; check the score after every step.

Input

The first line contains N and K. The second line contains the N values.

Output

Print the maximum score of a good subarray.

Constraints

1 <= N <= 100000; 1 <= NUMS[i] <= 2 × 10^4; 0 <= K < N

Example input

6 3
1 4 3 7 4 5

Example output

15

Explanation

For 1 4 3 7 4 5 with K = 3, the block from index 1 to 5 has minimum 3 and length 5, a score of 15, which is the best.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Constrained Subsequence Sum Next: Minimum Number of K Consecutive Bit Flips