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

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

hide comments
2020-01-29 05:52:19
My 50th. Basics of tree give you logic between tree leaves and number of nodes.
2019-04-16 15:22:33
easy one my 100th
2018-12-23 05:55:18
think simple it will work don't afraid of tree:)
2017-05-28 21:11:35
Easy One!!
My 100th :-)
2016-07-28 07:55:54
n/2 simple work :-)
2016-02-04 05:51:38 minhthai
remember heap ? :)
2015-05-07 21:01:27 Tony Stark
ok got it!

Last edit: 2015-05-07 21:03:46
2015-04-01 19:12:51 Asheesh Pathak
Just leave first n/2 nodes :)
2015-03-26 20:14:18 Rishabh Joshi
Sum of values of ALL THE LEADS, not just the deepest level. Cost me 2 WA.
2014-12-26 12:35:12 :.Mohib.:
Some paper work gives you a logic.....
AC in first go.... :)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.