Arithmetic Sequence Transformation
Time limit1sMemory limit512 MB
Each element of B can be changed by at most +1 or -1, and we want the cheapest way to make the whole sequence arithmetic.
- Level
Medium7 of 10
- Topics
- Math, Implementation, Brute force, Greedy
- Solved
- No attempts yet
Problem
A sequence of size is called arithmetic if is the same for every . For example, , , , and are arithmetic, while and are not.
We want to transform the sequence into an arithmetic sequence. Each number can have an operation applied to it at most once. There are two operations: add 1 or subtract 1. Find the minimum number of operations needed to transform the sequence into an arithmetic sequence.
Input
The first line gives the size of the sequence . The second line gives .
Output
Print the minimum number of operations needed to transform the sequence into an arithmetic sequence. If it cannot be transformed into an arithmetic sequence, print -1.