#K49532. Calculate Directory Size

    ID: 28663 Type: Default 1000ms 256MiB

Calculate Directory Size

Calculate Directory Size

You are given a JSON representation of a directory structure. The directory is represented as an object with two fields: type and contents. The type field will be "directory" for directories and "file" for files. For file objects, there is an additional integer field size indicating its size in bytes. For directory objects, the contents field is a list that may contain files and subdirectories.

Your task is to compute the total size of the directory, which is defined as the sum of the sizes of all files in the directory and in all nested subdirectories.

Mathematical Formulation:

The total size \( S(D) \) of a directory \( D \) is given by:

[ S(D) = \sum_{f \in F(D)} size(f) + \sum_{d \in C(D)} S(d), ]

where \( F(D) \) is the set of files directly contained in \( D \) and \( C(D) \) denotes the set of subdirectories of \( D \).

inputFormat

The input is provided via stdin as a single valid JSON string representing the directory structure. The JSON object has the following format:

{
  "type": "directory",
  "contents": [
    // List of files or directories
  ]
}

Each file is represented as:

{"type": "file", "size": integer}

Each directory is represented in the same manner as the main directory.

outputFormat

The output should be printed to stdout and is a single integer representing the total size (in bytes) of the directory including its subdirectories.

## sample
{"type": "directory", "contents": []}
0