#C10399. Maximum Nested Function Call Depth

    ID: 39599 Type: Default 1000ms 256MiB

Maximum Nested Function Call Depth

Maximum Nested Function Call Depth

You are given a log of events, where each event represents a function call or the end of a function call. Each event is represented as a string. A function call is indicated by any string not equal to "end", and the end of a function call is indicated by the string "end".

Your task is to determine the maximum nested depth of the function calls based on the log sequence. The nesting depth increases by 1 when a function call is encountered and decreases by 1 when an end event is encountered.

The maximum nested depth is defined as the highest value reached by the current nesting level during the processing of the log. If the log is empty, the maximum nested depth is 0.

The problem can be mathematically formulated as follows:

Let \(d\) be the current depth and \(m\) be the maximum depth.
\(d = 0\) initially. For each event \(e_i\) in the log:

  • If \(e_i \neq \texttt{\"end\"}\), then \(d \leftarrow d + 1\) and \(m \leftarrow \max(m, d)\).
  • If \(e_i = \texttt{\"end\"}\), then \(d \leftarrow d - 1\).
Output \(m\).

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer \(n\) representing the number of log entries.
  • The second line contains \(n\) space-separated strings, each representing a log entry.

If \(n = 0\), then there will be no log entries on the second line.

outputFormat

Output a single integer to stdout — the maximum nested depth of function calls computed from the log.

## sample
5
foo bar end baz end
2

</p>