This page is still under construction.

Parts of this page are still being built. What you see may change.

Logarithmic Paprika

Interview

Time limit1sMemory limit128 MB

Summary
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 20=12^0 = 1 gram, and the heaviest weighs 2k2^k 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 kk (1≤k≤101 \le k \le 10): the available paprika weights are 20,21,…,2k2^0, 2^1, \dots, 2^k grams. The second line contains k+1k + 1 integers p0,p1,…,pkp_0, p_1, \dots, p_k (0≤pi≤10000 \le p_i \le 1000), separated by single spaces, describing the current stock: there are p0p_0 paprika of weight 20=12^0 = 1 gram, p1p_1 of weight 21=22^1 = 2 grams, …\dots, and pkp_k of weight 2k2^k grams.

Output

Print a single positive integer xx: the smallest weight that cannot be assembled without cutting any paprika.

Hint

For example, suppose the stock holds two paprika of weight 11 gram, one of weight 22 grams, and one of weight 44 grams. Then every weight from 11 to 88 can be assembled: 1=11 = 1, 2=1+12 = 1 + 1, 3=1+23 = 1 + 2, 4=44 = 4, 5=1+45 = 1 + 4, 6=1+1+46 = 1 + 1 + 4, 7=1+2+47 = 1 + 2 + 4, 8=1+1+2+48 = 1 + 1 + 2 + 4. The value 99 cannot be assembled, so the answer for this case is 99.

Examples1

  1. Example 1

    Input
    2
    2 1 1
    
    Expected output
    9