【bzoj3427】Poi2013 Bytecomputer

2014年11月22日1,2620

Description

A sequence of N  integers I1,I2…In from the set {-1,0,1} is given. The bytecomputer is a device that allows the following operation on the sequence: incrementing I(i+1) by I(i) for any 1<=I<=N. There is no limit on the range of integers the bytecomputer can store, i.e., each I(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 I1<=I2<=…I(n)) with the minimum number of operations.
给定一个{-1,0,1}组成的序列,你可以进行x[i]=x[i]+x[i-1]这样的操作,求最少操作次数使其变成不降序列。

Input

The first line of the standard input holds a single integer N(1<=N<=1000000) , the number of elements in the (bytecomputer’s) input sequence.
The second line contains N  integers I1,I2…I(n) Ii from set {-1,0,1}  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.

Sample Input

6
-1 1 0 -1 0 1

Sample Output

3
Explanation of the example: with three operations, the bytecomputer can obtain the sequence -1,-1,-1,-1,0,1

题解

可以证明最终是一段-1,一段0,一段1。。。

这样就能用dp解决了