#K57942. Longest Strictly Increasing Path in an N-ary Tree

    ID: 30532 Type: Default 1000ms 256MiB

Longest Strictly Increasing Path in an N-ary Tree

Longest Strictly Increasing Path in an N-ary Tree

You are given a tree in an S-expression format. Each node in the tree contains an integer value and may have zero or more children. Your task is to find the length of the longest path from the root to any leaf such that the values along the path are strictly increasing.

The tree is provided in the following format:

  • If the tree is empty, the input is the string null.
  • If the tree is not empty, it is represented as an S-expression: value (child1 child2 ... childK). For instance, the tree with root 10 and two children 5 and 20 is given as: 10 (5 20). Children can themselves have subtrees, e.g., 10 (5 (3 7 15) 20 (25)).

The answer is a single integer: the length of the longest strictly increasing path starting at the root.

Note: A path consisting of a single node is considered to have length 1.

Examples:

Input: 10 (5 (3 7 15) 20 (25))
Output: 3

Input: null Output: 0

</p>

inputFormat

The input is a single line from stdin which encodes an n-ary tree in an S-expression format. If the tree is empty, the input will be the string null.

The format for a non-empty tree is:

value [(child1 child2 ... childK)]

Here, value is an integer, and if a node has children, they are enclosed in parentheses and separated by spaces. For example: 10 (5 (3 7 15) 20 (25)).

outputFormat

The output is a single integer printed to stdout, representing the length of the longest strictly increasing path from the root to any leaf.

## sample
null
0

</p>