#C4830. Minimum Watering Distance

    ID: 48412 Type: Default 1000ms 256MiB

Minimum Watering Distance

Minimum Watering Distance

Maria has a row of n trees, where each tree is represented by an integer: 1 if the tree requires watering and 0 otherwise. The trees are positioned consecutively with positions numbered from 0 to n-1. Maria will water all the trees that require watering by starting at the first tree needing water and finishing at the last such tree. The task is to calculate the minimum total distance she must walk, which can be expressed as:

\( \text{Distance} = x_{last} - x_{first} \)

If no tree requires watering, the distance is 0.

inputFormat

The input is given from stdin in the following format:

  • The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of trees.
  • The second line contains n space-separated integers, each being either 0 or 1, representing whether a tree requires watering.

outputFormat

Print the minimal total distance Maria needs to walk as an integer to stdout.

## sample
5
1 0 0 0 1
4

</p>