A reactor logs n per-cycle multiplier readings, one per line position. Each reading is an integer that may be negative (a phase inversion), zero (a shutdown), or positive. For a contiguous run of readings, its combined effect is the product of the readings in the run.
Find the maximum combined effect achievable over all non-empty contiguous runs of readings.
Line 1: the integer n.
Line 2: n space-separated integers r[0] r[1] ... r[n-1].
A single integer: the maximum product over all non-empty contiguous runs. The value may be large; print it exactly (no modulo).
Example 1
Input
4 2 3 -2 4
Expected
6
Explanation
The run [2, 3] gives 6, which is the largest product; extending to include -2 would turn it negative.
Example 2
Input
3 -2 0 -1
Expected
0
Explanation
Every multi-element run through 0 gives 0, and the best single reading is 0, so the maximum product is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →