Cheerio Contest 3 P4 - Bit Flips

View as PDF

Submit solution


Points: 12
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

You have an array a of length N. In one operation, you can choose a number in the array and flip one of its bits, as long as the resultant number is non-negative and strictly less than 2^{30}. What is the minimum number of operations required to turn the array into a non-decreasing sequence?

Constraints

For all subtasks:

2 \le N \le 100

0 \le a_i < 2^{30}

Input Specification

The first line contains a single integer N.

The second line contains N integers a_i.

Output Specification

Output the minimum number of operations required to turn the array into a non-decreasing sequence.

Sample Input

5
10 7 8 3 4

Sample Output

3

Explanation for Sample Output

A possible final array could be [2, 7, 8, 11, 12], which takes 3 moves to transition to.


Comments

There are no comments at the moment.