- Time limit: 0.50 s
- Memory limit: 128 MB
A sequence of n integers x_1, x_2, \ldots, x_n from the set \left\{-1, 0, 1\right\} is given. The bytecomputer is a device that allows the following operation on the sequence: incrementing x_{i+1} by x_i for any 1 \le i < n. There is no limit on the range of integers the bytecomputer can store, i.e., each x_i can (in principle) have arbitrarily small or large value.
Program the bytecomputer so that it transforms the input sequence into a non-decreasing sequence (i.e., such that x_1 \le x_2 \le \ldots, \le x_n) with the minimum number of operations.
Input
The first line of the standard input holds a single integer n, the number of elements in the (bytecomputer's) input sequence.
The second line contains n integers x_1, x_2, \ldots, x_n that are the successive elements of the (bytecomputer's) input sequence, separated by single spaces.
Output
The first and only line of the standard output should give one integer, the minimum number of operations the bytecomputer has to perform to make its input sequence non-decreasing, of the single word BRAK
(Polish for none) if obtaining such a sequence is impossible.
Constraints
- 1 \le n \le 10^6
- x_i \in \left\{-1, 0, 1\right\}
Example
For the input data:
6 -1 1 0 -1 0 1
the correct result is:
3
Explanation of the example: with three operations, the bytecomputer can obtain the sequence -1, -1, -1, -1, 0, 1.
Task author: Jacek Tomasiewicz.