UCV2013J - Valences


Mr. White has found a way to maximize the purity of crystals based on certain chemical compounds. He has observed that each compound is made of molecules that are linked together following the structure of a complete binary tree where every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Each node of the tree stores the valence of a molecule and is represented as an integer number. Mr. White uses an electronic microscope that dumps the molecule structure as a stream of integer numbers and would like to have your help on automatically obtaining the total valence of only the leaves of the given tree. For example, the sequence 4-3-2-6-0-3 represents the tree shown in the figure and the total valence of the leaves is 9.

 

Sample Tree

Input

The input contains several test cases, each one corresponding to a particular compound. Each test case consists of a single line starting with an integer N (1 ≤ N ≤ 1000000), followed by N integer numbers Vi representing the valences of each molecule separated by blank spaces (0 ≤ Vi ≤ 100).

The end of input is indicated by a test case with N = 0.

Output

For each compound output a single line with the sum of the valences of the leaves of the tree.

Example

Input:
6 4 3 2 6 0 3
7 1 1 1 2 1 2 1
0 Output: 9
6

hide comments
Shaka Shadows: 2013-07-23 20:54:44

My bad, problem is fine.

Shaka Shadows: 2013-07-23 20:48:52

Something is going wrong when judging this problem, despite the fact that there are some ACed codes.

pika_pika: 2013-07-23 19:53:45

my code runs within .16 s before giving WA as is with others, pls check the soulutions #id : 9706672 . runs fine for almost all kinds of test cases. (Runs in 0.01 s with fast I/O giving WA)

Anant Kumar: 2013-07-23 19:38:47

many of the solutions including mine seems to give wrong ans at .16 or .17 secs. Can someone give a test case which might be tricky.
EDIT: Finally accepted.
Read the question very carefully.

Last edit: 2013-07-24 12:35:07
Vipul Pandey: 2013-07-23 17:30:56

finally got AC after 6 useless WA. But it was not difficult at all.

Last edit: 2013-07-24 14:44:43
coding_express: 2013-07-23 16:24:07

too much points is being provided for an easy question


Added by:Hector Navarro
Date:2013-07-22
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Local UCV 2013. Walter Hernández