atcoder#ARC123D. [ARC123D] Inc, Dec - Decomposition

[ARC123D] Inc, Dec - Decomposition

Score : 700700 points

Problem Statement

Given is a sequence of integers A=(A1,,AN)A = (A_1, \ldots, A_N).

Consider a pair of sequences of integers B=(B1,,BN)B = (B_1, \ldots, B_N) and C=(C1,,CN)C = (C_1, \ldots, C_N) that satisfies the following conditions:

  • Ai=Bi+CiA_i = B_i + C_i holds for each 1iN1\leq i\leq N;
  • BB is non-decreasing, that is, BiBi+1B_i\leq B_{i+1} holds for each 1iN11\leq i\leq N-1;
  • CC is non-increasing, that is, CiCi+1C_i\geq C_{i+1} holds for each 1iN11\leq i\leq N-1.

Find the minimum possible value of $\sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr)$.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 108Ai108-10^8\leq A_i\leq 10^8

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

3
1 -2 3
10

One pair of sequences B=(B1,,BN)B = (B_1, \ldots, B_N) and C=(C1,,CN)C = (C_1, \ldots, C_N) that yields the minimum value is:

  • B=(0,0,5)B = (0, 0, 5),
  • C=(1,2,2)C = (1, -2, -2).

Here we have $\sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr) = (0+1) + (0+2) + (5+2) = 10$.

4
5 4 3 5
17

One pair of sequences BB and CC that yields the minimum value is:

  • B=(0,1,2,4)B = (0, 1, 2, 4),
  • C=(5,3,1,1)C = (5, 3, 1, 1).
1
-10
10

One pair of sequences BB and CC that yields the minimum value is:

  • B=(3)B = (-3),
  • C=(7)C = (-7).