Dynamic Programming21 sections · 916 units
Open in Course

Commando - Implementation

Handling maximization

For maximization: negate all values, find minimum, negate result. Or use upper envelope instead of lower. Upper envelope: the piecewise function giving maximum y at each x. Maintain with the same deque logic, but flip comparisons. Alternatively: insert lines (m,c)(-m, -c) into a lower envelope CHT, then negate the query result. Watch for integer overflow with large prefix sums. Use long long or 128-bit integers if needed.

Time complexity: O(n)O(n).

Space complexity: O(n)O(n) with monotonic CHT.