#C4630. Minimum Cuts to Adjust Tree Heights

    ID: 48190 Type: Default 1000ms 256MiB

Minimum Cuts to Adjust Tree Heights

Minimum Cuts to Adjust Tree Heights

You are given n trees in a row, where the height of the i-th tree is given by an integer. Your task is to perform "cuts" on the trees so that for every two adjacent trees, the absolute difference in their heights satisfies \(|h_i - h_{i-1}| \le 1\). In one cut, you can reduce the height of a tree by exactly 1 unit. Determine the minimum number of cuts required to satisfy this condition for the entire row of trees.

Note: When you perform a cut on a tree, its height decreases and may help in reducing the difference with its neighbor. The goal is to achieve \(|h_i - h_{i-1}| \le 1\) for all \(1 \le i < n\) with the minimum total number of cuts.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains an integer \(n\) representing the number of trees.
  2. The second line contains \(n\) space-separated integers representing the heights of the trees.

outputFormat

Output a single integer to standard output (stdout) indicating the minimum number of cuts required so that the absolute difference between the heights of every pair of adjacent trees is at most 1.

## sample
6
5 7 3 6 5 8
5