cswork / OA打开练习工作台

MetaOA #21

Nested List Weight Sum (for E4)

估计中等可在线判题

考点(AI 标注)树的遍历单调栈与堆

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.

输入格式

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 one integer.

样例 1

输入:

[[1,1],2,[1,1]]

输出:

10

样例 2

输入:

[1,[4,[6]]]

输出:

27

样例 3

输入:

[0]

输出:

0

样例说明

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.