Double and Add

Starting from an all-zero array, find the minimum number of single-element increments and whole-array doublings that produce the target array B.

Medium5GreedyBit manipulationMathImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

You have an array AA of length NN whose every element is 0. You can perform these two operations.

  • Increase one element of the array by 1.
  • Double every element of the array.

Given an array BB, write a program that finds the minimum number of operations needed to make AA equal to BB.

Input

The first line contains the size of the array, NN. (1N501 \le N \le 50)

The second line contains the NN elements of BB, separated by spaces. Each element is an integer between 0 and 1,000, inclusive.

Output

Print the minimum number of operations that turns AA into BB on the first line.