#P4523. Transforming a Histogram into a Roof

    ID: 17769 Type: Default 1000ms 256MiB

Transforming a Histogram into a Roof

Transforming a Histogram into a Roof

You are given a histogram consisting of N columns with heights \(X_1, X_2, \dots, X_N\). You need to transform this histogram into a roof by a series of operations. A roof is defined as a histogram having the following properties:

  • A single column is the top of the roof. Assume it is at position \(i\) with height \(h_i\).
  • The height of the column at position \(j\) is given by \(h_j = h_i - |i - j|\).
  • All heights \(h_j\) must be positive integers.

An operation consists of increasing or decreasing the height of a column by \(1\). Your task is to determine the minimal number of operations required to transform the given histogram into a roof.

Note: Ensure that after transformation, the condition \(h_i - |i-j| \ge 1\) holds for every column \(j\); hence \(h_i\) must be at least \(\max(i-1, N-i)+1\).

inputFormat

The first line contains a single integer \(N\) representing the number of columns in the histogram.

The second line contains \(N\) space-separated integers \(X_1, X_2, \dots, X_N\) representing the heights of the columns.

outputFormat

Output a single integer, the minimal number of operations needed to transform the histogram into a roof.

sample

3
3 2 3
2