#d11105. 不朴素的数列(Bonus)
不朴素的数列(Bonus)
Not-So-Plain Sequence (Bonus)
Problem Description
This problem is an enhanced version of http://oj.daimayuan.top/problem/882; the only difference lies in the ranges of and (highlighted in red in the original statement).
For a sequence , define .
We know that the operation is associative: , so can be computed easily.
However, Patricky thinks sequences with are too plain. Given , he wants you to modify using the following operations so that :
- Choose (), change to .
- Choose (), change to .
Find the minimum number of operations to make , or report that it is impossible.
Input Format
The input consists of two lines. The first line contains an integer , the size of .
The next line contains space-separated integers, representing .
Output Format
Output the minimum number of operations to make . If impossible, output on a single line.
Sample Input 1
3
4 8 5
Sample Output 1
9
Explanation: $[4, 8, 5] \xrightarrow{4} [0, 12, 5] \xrightarrow{5} [0, 17, 0]$.
Sample Input 2
4
0 5 15 10
Sample Output 2
0
Sample Input 3
6
1 2 3 4 5 6
Sample Output 3
2
Explanation: $[1, 2, 3, 4, 5, 6] \to [0, 3, 3, 4, 5, 6] \to [0, 3, 3, 3, 6, 6]$.
Constraints
- , but it is guaranteed that , i.e. the elements are not all zero.