#P8681. Maximum Level Sum in a Complete Binary Tree

    ID: 21847 Type: Default 1000ms 256MiB

Maximum Level Sum in a Complete Binary Tree

Maximum Level Sum in a Complete Binary Tree

You are given a complete binary tree with \(N\) nodes. Each node has a weight, and the nodes are given in level order as \(A_1, A_2, \cdots, A_N\). The tree is structured as shown in the image below:

Complete Binary Tree

Your task is to compute the sum of node weights for each depth (level) of the tree. Output the depth at which the sum is maximum. If there are multiple depths with the same maximum sum, output the smallest depth.

Note: The depth of the root is \(1\).

inputFormat

The input contains two lines:

  • The first line contains an integer \(N\) representing the number of nodes in the tree.
  • The second line contains \(N\) space-separated integers \(A_1, A_2, \ldots, A_N\), which are the weights of the nodes in level order (from top to bottom, left to right).

outputFormat

Output a single integer representing the depth which has the maximum sum of node weights. If there are multiple such depths, output the smallest one.

sample

7
1 2 3 4 5 6 7
3