s and a DP array dp of length n (number of shelves).i from 0 to n - 1.
i, pop the stack.dp[i] as the sum of the arithmetic progression from 0 to i.j be the top of the stack. Calculate dp[i] as dp[j] + calculateSum(j + 1, i).i onto the stack.dp array.