Wonderful Jump
Contest: Codeforces Round 842 (Div. 2)
Difficulty rating: 2900
Tags: dp, greedy

You are given an array of positive integers $$$a_1,a_2,\ldots,a_n$$$ of length $$$n$$$.

In one operation you can jump from index $$$i$$$ to index $$$j$$$ ($$$1 \le i \le j \le n$$$) by paying $$$\min(a_i, a_{i + 1}, \ldots, a_j) \cdot (j - i)^2$$$ eris.

For all $$$k$$$ from $$$1$$$ to $$$n$$$, find the minimum number of eris needed to get from index $$$1$$$ to index $$$k$$$.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 4 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots a_n$$$ ($$$1 \le a_i \le n$$$).

Output format:
Output $$$n$$$ integers — the $$$k$$$-th integer is the minimum number of eris needed to reach index $$$k$$$ if you start from index $$$1$$$.

Constraints:
Time limit: 4.0 s
Memory limit: 128.0 MB

Note:
In the first example:

- From $$$1$$$ to $$$1$$$: the cost is $$$0$$$,
- From $$$1$$$ to $$$2$$$: $$$1 \rightarrow 2$$$ — the cost is $$$\min(2, 1) \cdot (2 - 1) ^ 2=1$$$,
- From $$$1$$$ to $$$3$$$: $$$1 \rightarrow 2 \rightarrow 3$$$ — the cost is $$$\min(2, 1) \cdot (2 - 1) ^ 2 + \min(1, 3) \cdot (3 - 2) ^ 2 = 1 + 1 = 2$$$.

In the fourth example from $$$1$$$ to $$$4$$$: $$$1 \rightarrow 3 \rightarrow 4$$$ — the cost is $$$\min(1, 4, 4) \cdot (3 - 1) ^ 2 + \min(4, 4) \cdot (4 - 3) ^ 2 = 4 + 4 = 8$$$.

Example input:
3
2 1 3

Example output:
0 1 2

Example input:
6
1 4 1 6 3 2

Example output:
0 1 2 3 6 8

Editorial:
There is a very easy $$$\mathcal{O}(n^2)$$$ dp solution, we will show one possible way to optimize it to $$$\mathcal{O}(n \cdot \sqrt{A})$$$, where $$$A$$$ is the maximum possible value of $$$a_i$$$.
Let $$$dp_k$$$ be the minimum number of eris required to reach index $$$k$$$, $$$dp_1 = 0$$$.
Suppose we want to calculate $$$dp_j$$$ and we already know $$$dp_1, dp_2, \ldots, dp_{j - 1}$$$. Let's look at our cost function more closely.
We can notice that it is definitely not optimal to use the transition $$$i \rightarrow j$$$ if $$$\min(a_i \ldots a_j) \cdot (j - i)^2 > A \cdot (j - i)$$$. That is, it will be more optimal to perform $$$j - i$$$ jumps of length 1.
Transforming this inequality, we get $$$j - i > \frac{A}{\min(a_i \ldots a_j)}$$$. So if $$$\min(a_i \ldots a_j)$$$ is quite large, we only need to look at a couple of $$$i$$$ close to $$$j$$$, and then do something else for the small values of $$$\min(a_i \ldots a_j)$$$.
1. $$$\min(a_i \ldots a_j) \ge \sqrt{A}$$$
To handle this case, we can just iterate over all $$$i$$$ from $$$\max(j - \sqrt{A}, 1)$$$ to $$$j - 1$$$, since the transition $$$i \rightarrow j$$$ could be optimal only if $$$j - i \le \frac{A}{min(a_i \ldots a_j)} \le \sqrt{A}$$$.
Time complexity: $$$\mathcal{O}(\sqrt{A})$$$.
2. $$$\min(a_i \ldots a_j) < \sqrt{A}$$$
Another useful fact is that if there exists an index $$$k$$$ such that $$$i < k < j$$$ and $$$a_k = \min(a_i \ldots a_j)$$$, the transition $$$i \rightarrow j$$$ also cannot be optimal, since $$$i \rightarrow k$$$ followed by $$$k \rightarrow j$$$ will cost less.
Proof: $$$$$$\min(a_i \ldots a_j) \cdot (j - i) ^ 2 > \min(a_i \ldots a_k) \cdot (k - i) ^ 2 + \min(a_k \ldots a_j) \cdot (j - k)^2$$$$$$ $$$$$$a_k \cdot (j - i) ^ 2 > a_k \cdot (k - i) ^ 2 + a_k \cdot (j - k)^2$$$$$$ $$$$$$(j - i) ^ 2 > (k - i) ^ 2 + (j - k)^2$$$$$$
This leaves us two subcases two handle.
2.1 $$$\min(a_i \ldots a_j) = a_i$$$
Just maintain the rightmost occurrences $$$ < j$$$ of all values from $$$1$$$ to $$$\sqrt{A}$$$. Time complexity: $$$\mathcal{O}(\sqrt{A})$$$.
2.2 $$$\min(a_i \ldots a_j) = a_j$$$
Initially set $$$i$$$ to $$$j - 1$$$ and decrease it until $$$a_i \le a_j$$$ becomes true. Time complexity: $$$\mathcal{O}(\sqrt{A})$$$ amortized.
Total time complexity: $$$\mathcal{O}(n \cdot \sqrt{A})$$$