#abc307g. G - Approximate Equalization

G - Approximate Equalization

Score : 550550 points

问题描述

给定一个长度为 NN 的整数序列:A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N)

高桥可以按照任意顺序执行以下两种操作任意次,包括零次。

  • 选择一个整数 ii,满足 1iN11\leq i\leq N-1,将 AiA_i11 并将 Ai+1A_{i+1}11
  • 选择一个整数 ii,满足 1iN11\leq i\leq N-1,将 AiA_i11 并将 Ai+1A_{i+1}11

求解使得序列 AA 满足以下条件所需的最小操作次数:

  • 对于任何一对在 11NN 之间的整数 (i,j)(i,j),有 AiAj1\lvert A_i-A_j\rvert\leq 1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given an integer sequence of length NN: A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N).

Takahashi can perform the following two operations any number of times, possibly zero, in any order.

  • Choose an integer ii such that 1iN11\leq i\leq N-1, and decrease AiA_i by 11 and increase Ai+1A_{i+1} by 11.
  • Choose an integer ii such that 1iN11\leq i\leq N-1, and increase AiA_i by 11 and decrease Ai+1A_{i+1} by 11.

Find the minimum number of operations required to make the sequence AA satisfy the following condition:

  • AiAj1\lvert A_i-A_j\rvert\leq 1 for any pair (i,j)(i,j) of integers between 11 and NN, inclusive.

Constraints

  • 2N50002 \leq N \leq 5000
  • Ai109\lvert A_i \rvert \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print a single line containing the minimum number of operations required to make the sequence AA satisfy the condition in the problem statement.

Sample Input 1

3
2 7 6

Sample Output 1

4

You can make AA satisfy the condition with four operations as follows.

  • Choose i=1i=1, and increase A1A_1 by 11 and decrease A2A_2 by 11, making A=(3,6,6)A=(3,6,6).
  • Choose i=1i=1, and increase A1A_1 by 11 and decrease A2A_2 by 11, making A=(4,5,6)A=(4,5,6).
  • Choose i=2i=2, and increase A2A_2 by 11 and decrease A3A_3 by 11, making A=(4,6,5)A=(4,6,5).
  • Choose i=1i=1, and increase A1A_1 by 11 and decrease A2A_2 by 11, making A=(5,5,5)A=(5,5,5).

This is the minimum number of operations required, so print 44.

Sample Input 2

3
-2 -5 -2

Sample Output 2

2

You can make AA satisfy the condition with two operations as follows:

  • Choose i=1i=1, and decrease A1A_1 by 11 and increase A2A_2 by 11, making A=(3,4,2)A=(-3,-4,-2).
  • Choose i=2i=2, and increase A2A_2 by 11 and decrease A3A_3 by 11, making A=(3,3,3)A=(-3,-3,-3).

This is the minimum number of operations required, so print 22.

Sample Input 3

5
1 1 1 1 -7

Sample Output 3

13

By performing the operations appropriately, with 1313 operations you can make A=(0,0,1,1,1)A=(0,0,-1,-1,-1), which satisfies the condition in the problem statement. It is impossible to satisfy it with 1212 or fewer operations, so print 1313.

update @ 2024/3/10 08:42:35