P5551 Chino's Tree Study

Background

Chino really likes trees.

Description

A Chino tree is a full binary tree with a certain property. Specifically, for every non-leaf node in this tree, the value of the right child $(C)$ of its left child $(A)$ is the same as the value of the left child $(D)$ of its right child $(B)$. Also, the subtrees rooted at $C$ and $D$ are exactly the same. Now, Chino wants to know how to walk from the root to any leaf node so that the sum of the node weights along the path is as large as possible. ![](https://cdn.luogu.com.cn/upload/pic/23672.png ) $Orz yky,dyh,wjk,jjy,cxr,gsy,cpy,zcy,tyz,yy,hz,zhr,ygg$

Input Format

The first line contains an integer $n$, which represents the number of levels of the binary tree. The second line contains $2^n-1$ numbers, which represent the preorder traversal of the binary tree.

Output Format

One line containing one number, which is the maximum possible sum of node weights.

Explanation/Hint

![](https://cdn.luogu.com.cn/upload/pic/23674.png ) ![](https://cdn.luogu.com.cn/upload/pic/23675.png ) ![](https://cdn.luogu.com.cn/upload/pic/23673.png ) A long long savior. Translated by ChatGPT 5