## Cheerio Contest 3 P4 - Bit Flips

View as PDF

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

Author:
Problem type

You have an array of length . 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 . What is the minimum number of operations required to turn the array into a non-decreasing sequence?

#### Input Specification

The first line contains a single integer .

The second line contains integers .

#### 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 , which takes moves to transition to.