#C12739. Merge Two Dictionaries

    ID: 42199 Type: Default 1000ms 256MiB

Merge Two Dictionaries

Merge Two Dictionaries

You are given two dictionaries represented in JSON format. Each dictionary contains keys (strings) and integer values. Your job is to merge these two dictionaries into one. If a key exists in both dictionaries, the resulting dictionary should contain the same key with its value equal to the sum of the values from both dictionaries. Otherwise, the key and its value should appear as they are.

In mathematical terms, if we denote the value for key k in dictionary 1 as \(a_k\) and in dictionary 2 as \(b_k\), then the merged dictionary will have the key k with value \(a_k + b_k\) (if \(k\) exists in both) or simply \(a_k\) or \(b_k\) if it exists in only one.

Please note that the input is read from standard input (stdin) and the result must be printed to standard output (stdout) as a JSON formatted dictionary.

inputFormat

The input consists of two lines. Each line is a valid JSON string representing a dictionary. For example:

{"a":1,"b":2}
{"b":3,"c":4}

You must parse these two dictionaries, merge them as described, and then output the resulting dictionary in JSON format.

outputFormat

Output a single line containing a JSON formatted dictionary representing the merged dictionary. Keys that appear in both dictionaries will have their values summed. For instance, the example above should produce:

{"a":1,"b":5,"c":4}
## sample
{"a":1,"b":2}
{"b":3,"c":4}
{"a":1,"b":5,"c":4}