Pick The Optimization

dp[i][j]=min⁡k<j{dp[i−1][k]+(pre[j]−pre[k])2}dp[i][j] = \min_{k < j} { dp[i-1][k] + (pre[j] - pre[k])^2 }, with ii up to 200200, jj up to 10510^5, and all array values non-negative. Cheapest correct route?

A

The Aliens trick on ii, one binary search per penalty. Expanding the square makes each kk a line in pre[j]pre[j] with slope −2 pre[k]-2,pre[k] and intercept dp[i−1][k]+pre[k]2dp[i-1][k] + pre[k]^2. A penalty per layer drops the ii dimension entirely, so one hull is O(n)O(n) per probe and O(nlog⁡V)≈6⋅106O(n \log V) \approx 6 \cdot 10^6 overall.

B

Convex hull trick, one hull per layer. Expanding the square makes each kk a line in pre[j]pre[j] with slope −2 pre[k]-2,pre[k] and intercept dp[i−1][k]+pre[k]2dp[i-1][k] + pre[k]^2. Non-negative values make both the slopes and the query points monotone, so a deque hull is O(n)O(n) per layer and O(mn)≈2⋅107O(mn) \approx 2 \cdot 10^7 overall.

C

Divide and conquer DP, one recursion per layer. Expanding the square makes each kk a line in pre[j]pre[j] with slope −2 pre[k]-2,pre[k] and intercept dp[i−1][k]+pre[k]2dp[i-1][k] + pre[k]^2. It is the only option here, since a squared cost is not Monge, so the recursion is O(mnlog⁡n)≈3⋅108O(mn \log n) \approx 3 \cdot 10^8 overall.

D

Knuth optimization, one interval table per layer. Expanding the square makes each kk a line in pre[j]pre[j] with slope −2 pre[k]-2,pre[k] and intercept dp[i−1][k]+pre[k]2dp[i-1][k] + pre[k]^2. Quadrangle monotonicity collapses the split range, so the whole table is O(n2)≈1010O(n^2) \approx 10^{10} overall and fits the limit.