#P2893. [USACO08FEB] Making the Grade G

[USACO08FEB] Making the Grade G

Problem Description

Farmer John wants to renovate a road of length NN. The original elevation of each segment is AiA_i, and after repair it becomes BiB_i, with a cost of AiBi|A_i - B_i|. We require the repaired road, that is, the sequence BiB_i you construct, to be monotone nonincreasing or monotone nondecreasing. Find the minimum total cost i=1NAiBi\sum_{i=1}^{N} |A_i - B_i|.

Input Format

The first line contains an integer NN.

The next NN lines each contain an integer AiA_i.

Output Format

Output a single integer, the minimum cost.

7
1
3
2
4
5
3
9

3

Hint

Constraints: 1N20001 \le N \le 2000, 0Ai1090 \le A_i \le 10^9.

Translated by ChatGPT 5