Double and Add
InterviewTime limit2sMemory limit512 MB
Starting from an all-zero array, find the minimum number of single-element increments and whole-array doublings that produce the target array B.
- Level
Medium5 of 10
- Topics
- Greedy, Bit manipulation, Math, Implementation
- Solved
- No attempts yet
Problem
You have an array of length 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 , write a program that finds the minimum number of operations needed to make equal to .
Input
The first line contains the size of the array, . ()
The second line contains the elements of , separated by spaces. Each element is an integer between 0 and 1,000, inclusive.
Output
Print the minimum number of operations that turns into on the first line.