#P10067. [CCO 2023] Real Mountains

[CCO 2023] Real Mountains

Problem Description

With your help, Rebecca’s landscape photo has made it onto the cover of the latest issue of her magazine. However, some readers still seem unhappy with the photo. In particular, they think the mountains in the photo are fake.

For simplicity, we can describe the photo as a sequence of NN columns of pixels. In column ii, the bottom hih_{i} pixels are mountain. Her readers will only believe this is a real mountain if the photo contains a single peak. That is, if there exists an index pp with 1pN1 \leq p \leq N such that $h_{1} \leq h_{2} \leq \cdots \leq h_{p} \geq \cdots \geq h_{N-1} \geq h_{N}$.

Luckily, Rebecca can also pay her editors to modify the photo and reprint the magazine. Unfortunately, the editors have a very strange pricing scheme for their work. The only way Rebecca can edit the photo is to send her editors an email containing three integers (i,j,k)(i, j, k) satisfying 1i<j<kN1 \leq i<j<k \leq N and hi>hj<hkh_{i}>h_{j}<h_{k}. The editors will add one extra mountain pixel to column jj (i.e., increase hjh_{j} by 11), at a cost of hi+hj+hkh_{i}+h_{j}+h_{k}. Note that changes to hjh_{j} may affect the cost of future edits.

To satisfy her readers, Rebecca wants to edit the photo so that they believe there is a real mountain here. Can you tell her the minimum total cost required?

Input Format

The first line contains an integer NN.

The second line contains NN space-separated integers, denoting h1,h2,,hNh_{1}, h_{2}, \ldots, h_{N}.

Output Format

Output Tmod(106+3)T \bmod (10^{6}+3), where TT is the minimum total cost Rebecca needs to pay to satisfy her readers.

8
3 2 4 5 4 1 2 1
14

Hint

Rebecca can send two emails. The first contains (2,6,7)(2,6,7), and the second contains (1,2,5)(1,2,5). The first email costs 55 and increases h6h_{6} by 11, and the second email costs 99 and increases h2h_{2} by 11.

In the end, the hih_{i} values in the photo will be [3,3,4,5,4,2,2,1][3,3,4,5,4,2,2,1].

For all testdata, 3N1063\leq N \leq 10^6 and 1hi1091 \leq h_{i} \leq 10^{9}.

Subtask ID Score Range of NN Range and constraints on hih_{i}
1 12 N5000N \leq 5000 $1 \leq h_{i} \leq 100, \exists p \in [1,N], h_{1} \geq h_{2} \geq \cdots \geq h_{p} \leq \cdots \leq h_{N-1} \leq h_{N}$
2 1hi1001 \leq h_{i} \leq 100
3 1hi1061 \leq h_{i} \leq 10^{6}
4 1hi1091 \leq h_{i} \leq 10^{9}
5 16 N106N \leq 10^{6} 1hi1001 \leq h_{i} \leq 100
6 20 1hi1061 \leq h_{i} \leq 10^{6}
7 16 1hi1091 \leq h_{i} \leq 10^{9}

Translated by ChatGPT 5