Bytecomputer
Time limit3sMemory limit512 MB
Repeatedly add an entry to its right neighbor in a -1, 0, 1 sequence to make it non-decreasing with the fewest operations, or report BRAK if impossible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
You are given a sequence of integers , each of them from the set . The bytecomputer is a device that allows one operation on the sequence: choose an index with and increase by . The range of integers the bytecomputer can store is unlimited, so in principle each can become arbitrarily small or arbitrarily large.
You want the sequence to be non-decreasing, that is, . Find the minimum number of operations needed.
Input
The first line contains one integer , the length of the sequence ().
The second line contains the elements of the sequence in order, separated by single spaces ().
Output
Print on one line the minimum number of operations that make the sequence non-decreasing. If no sequence of operations achieves that, print BRAK instead. BRAK is Polish for none.
Hint
Three operations turn the sequence into .