Maximum Path Sum


Problem Statement

You are given a complete binary tree consisting of nn nodes of non-negative integers.

Find the maximum sum of all root-to-leaf paths.

Input

Your first line will contain nn.

Your next line will contain nn numbers, representing the complete binary tree.

Output

The maximum sum of all root-to-leaf paths.

Constraints

For all test cases:

  • 0≤n≤1060 \le n \le 10^6
  • Each node value, nin_i, is restricted between 0≤ni≤1060 \le n_i \le 10^6.

Example 1

Input 1
3
4 6 5
Output 1
10

Example 2

Input 2
6
5 3 7 11 4 2
Output 2
19

Example 3

Input 3
0

Output 3
0

Python Template

Code 1
n = int(input())
tree = list(map(int, input().split()))

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.