MetaOA #21
Nested List Weight Sum (for E4)
Estimated MediumJudge-ready
Topics (AI-labelled)Tree TraversalStacks & Heaps
The elements of a nested list are integers or lists. Integers in the outermost list have depth 1, and each additional level of list nesting adds 1 to the depth. Output the sum of every integer multiplied by its depth; negative numbers and empty sublists are allowed.
Input
One line with a valid JSON nested list whose outermost level has 1..50 elements; each integer is in −100..100, and no integer is deeper than 50. This site limits the total number of list and integer nodes to at most 100000, and the input to at most 2000000 characters.
Output
Output one integer.
Example 1
Input:
[[1,1],2,[1,1]]
Output:
10
Example 2
Input:
[1,[4,[6]]]
Output:
27
Example 3
Input:
[0]
Output:
0
Example notes
Example 1: the four 1s have depth 2 and the integer 2 has depth 1, so the sum is 4×1×2+2×1=10.
Example 2: 1, 4, and 6 have depths 1, 2, and 3 respectively, so the sum is 1+8+18=27.
Example 3: the only integer, 0, contributes 0×1=0.
Editorial
Loading solution…