395. Level Sums and Widest Level
MediumBFS
Print the sum of each level on one line, then on the next line print the 1-based level with the maximum sum (the smallest such level if tied).
Input format
Line 1: n (number of level-order entries) Line 2: the values in level order, -1 for a missing child
Output format
Line 1: level sums Line 2: best level
Example 1
Input
7 1 7 0 7 -8 -1 -1
Output
1 7 -1 2
Example 2
Input
9 989 -1 10250 98693 -89388 -1 -1 -1 -32127
Output
989 10250 9305 -32127 2
Constraints
1 ≤ nodes ≤ 10⁴
Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.
Loading the MyLoginn IDE…