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
agaurav77: 2014-06-03 16:25:50

AC, yeah!
Those people with WA: Your hint is "leaf". Ponder on the word. If you don't get it draw the diagram for HandSome's test case. Sometimes we miss the obvious fact.

Prashant Kiran: 2013-12-22 21:27:28

Easy one...AC in first shot :)..no knowledge of trees required

Last edit: 2013-12-22 21:27:56
..........: 2013-12-20 18:09:45

how to make diffrence between space and line in code

H@nDsOMè`_DèV!`L: 2013-09-19 23:02:25

9 1 2 3 1 2 3 4 1 1
11

Mohammed Hafez: 2013-09-03 21:40:31

I keep getting wrong answer. Even after considering spaces between numbers!
Any tricky test cases please

The Mundane Programmer: 2013-08-24 21:14:20

AC in 1st attempt..... Easy 1 :)

Alien: 2013-08-19 05:32:42

easy1..

auror: 2013-08-11 17:03:12

test cases anyone..
Getting WA

CoNtRaDiCtIoN: 2013-08-07 20:25:20

read the question carefully !!!


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