Logarithmic Paprika
InterviewTime limit1sMemory limit128 MB
Given counts of paprika weighing 1, 2, 4, ..., 2^k grams, find the smallest positive weight that cannot be formed from whole pieces.
- Level
Medium5 of 10
- Topics
- Greedy, Math, Bit manipulation, Sorting
- Solved
- No attempts yet
Problem
The best-selling vegetable in Byteland is the logarithmic paprika. As its name suggests, every paprika weighs a power of two grams. The lightest paprika weighs gram, and the heaviest weighs grams.

The residents of Byteland dislike buying pieces of a paprika, so sellers must sell only whole paprika. On top of that, the locals are very particular: they will not tolerate a seller who cannot hand over exactly the weight they want to buy. This causes a great deal of stress among the sellers.
A friend of yours who runs a vegetable garden has asked you to write a program to help the sellers. Write a program that:
- reads the current paprika stock from standard input,
- determines the smallest weight that cannot be assembled without cutting any paprika,
- writes the result to standard output.
Input
The first line contains one integer (): the available paprika weights are grams. The second line contains integers (), separated by single spaces, describing the current stock: there are paprika of weight gram, of weight grams, , and of weight grams.
Output
Print a single positive integer : the smallest weight that cannot be assembled without cutting any paprika.
Hint
For example, suppose the stock holds two paprika of weight gram, one of weight grams, and one of weight grams. Then every weight from to can be assembled: , , , , , , , . The value cannot be assembled, so the answer for this case is .