#P2893. [USACO08FEB] Making the Grade G

    ID: 1936 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp递推2008USACOO2优化

[USACO08FEB] Making the Grade G

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