#C12933. Sum of Unique Elements

    ID: 42415 Type: Default 1000ms 256MiB

Sum of Unique Elements

Sum of Unique Elements

You are given a possibly nested data structure which may include arrays (lists or tuples) and objects (dictionaries). The structure contains integers along with other types. Your task is to recursively traverse the structure and compute the sum of all unique integers.

If the optional flag is enabled, any negative integers should be treated as zero before being considered for the summation.

The final sum should be computed as \[ S = \sum_{x \in U} x, \] where \(U\) is the set of unique integers after applying the negative treatment (if enabled).

Note: Non-integer elements must be ignored in the summation.

inputFormat

The input is provided via standard input (stdin) and consists of two lines:

  1. treat_neg_as_zero: A boolean flag given as either true or false (case-insensitive). When true, negative integers should be treated as zero.
  2. A JSON formatted string representing the nested data structure. The structure may include arrays (used for lists/tuples) and objects (used for dictionaries).

For example:

false
[1, [2, 3], [4, [5]]]

outputFormat

Output a single integer to standard output (stdout) which is the sum of all unique integers discovered in the input data structure, after applying the negative conversion (if applicable).

## sample
false
[1, 2, 3, 4]
10