object NestedListWeightSum extends App { // Define a simple data structure to represent NestedInteger sealed trait NestedInteger case class NestedList(value: List[NestedInteger]) extends NestedInteger case class NestedIntegerValue(value: Int) extends NestedInteger // Example nested list val nestedList = List( NestedIntegerValue(1), NestedList(List( NestedIntegerValue(2), NestedIntegerValue(3) )), NestedIntegerValue(4) ) def depthSum(nestedList: List[NestedInteger], depth: Int = 1): Int = { nestedList.foldLeft(0) { (acc, ni) => ni match { case NestedList(value) => acc + depthSum(value, depth + 1) case NestedIntegerValue(value) => acc + value * depth } } } // Calculate the nested list weight sum val result = depthSum(nestedList) println(s"Nested List Weight Sum: $result") }