862. The Best Stretch That Covers a Given Position
Read n positive values and a 1-based position p. The SCORE of a contiguous stretch is its
smallest value multiplied by the number of values it holds. Print the largest score over the
stretches that CONTAIN position p.
n = 6, p = 3, values 1 4 3 7 4 5 -> 15
n = 8, p = 1, values 5 5 4 5 4 1 1 1 -> 20
n = 5, p = 3, values 1 1 1 1 1 -> 5
In the first row the best stretch is 3 7 4 5, whose minimum is 3 and whose length is 4.
## Grouping the stretches by their minimum
There are O(n^2) stretches covering p, so they cannot be enumerated. Group them instead by
which position holds their minimum.
For a group belonging to position i, all members share the minimum a[i] and differ only in
length, so the best member is the longest, which is the widest window in which a[i] is still a
minimum:
lo = previousStrictlySmaller(i) + 1
hi = nextStrictlySmaller(i) - 1
Both boundaries strict, since an equal value does not lower the minimum.
So the candidate list is one entry per position:
if lo <= p-1 <= hi: score(i) = a[i] * (hi - lo + 1)
otherwise: i offers nothing, because its window misses p
and the answer is the largest candidate.
## Why the filter is exactly right
Take any stretch [l, r] containing p and let i hold its minimum. Then [l, r] is inside
i's widest window, so that window contains p too and therefore passes the filter, and its
score is at least the score of [l, r] since the minimum is the same and the length is larger.
Conversely every candidate that passes the filter is a genuine stretch containing p. So the
maximum over candidates equals the true answer.
The filter is what makes this different from problem 855: there, every window is admissible and
the score involves the TOTAL; here the score involves the LENGTH and only windows straddling p
count. Both reductions come from the same grouping.
## Why the constraint is not just an annoyance
Without it, the answer is a global maximum and the problem is the largest rectangle in a histogram. With it, there is no monotone structure to exploit along the array, and grouping by minimum is the only reduction that survives. Recognising which parts of a technique still apply when a constraint is added is more useful than the technique itself.
## Size
a[i] reaches 2 * 10^4 and n reaches 10^5, so the product reaches 2 * 10^9.
A signed 32 bit integer holds up to 2147483647, so that FITS, with about seven percent to
spare. Widening to 64 bits here is margin, not correctness.
## Related problems
Problem 855 is the same grouping with the stretch total instead of its length and with no position to cover. Problem 838 computes exactly these widest windows.
## Sizes and widths
The product reaches 2 * 10^4 * 10^5 = 2 * 10^9, which is INSIDE a signed 32 bit
integer by a small margin, since the limit is 2147483647. The margin is the interesting
part: it is about seven percent, so a value cap of 22000 instead of 20000 would push
the same product over the edge. That is why the figure gets computed rather than recognised.
## Cost
Time O(n), two stack passes and one linear maximum. Space O(n).
Constraints - `1 ≤ p ≤ n ≤ 100000` - `1 ≤ a[i] ≤ 20000` - the answer can reach `2 * 10^9`, which still fits in 32 bits; see the statement
Input
The first line contains two integers n and p, where p is a 1-based position.
The second line contains n integers a[0] ... a[n-1].
Output
Print one integer alone on a line: the largest value of (stretch minimum) times (stretch length) over all contiguous stretches that contain position p. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.