#C12449. Aggregate Tuples

    ID: 41877 Type: Default 1000ms 256MiB

Aggregate Tuples

Aggregate Tuples

Given a list of tuples, each containing a string key and an integer value, your task is to aggregate the values by their corresponding keys. For each key \(s\) that appears in the input, compute the sum of all integers \(v\) such that the tuple \((s, v)\) appears in the input. In other words, for every distinct key, calculate:

\(\text{result}[s] = \sum_{i: tuple_i[0]=s} tuple_i[1]\)

The final result should be represented in the JSON object format with keys sorted in lexicographical order. For example, if the aggregated result is {"apple": 6, "banana": 3, "orange": 1}, then the output should be exactly:

{"apple":6,"banana":3,"orange":1}

inputFormat

The first line of the input contains a single integer \(n\) (\(0 \leq n \leq 10^5\)) representing the number of tuples. Each of the following \(n\) lines contains a string and an integer separated by a space.

For example:

4
apple 2
banana 3
apple 4
orange 1

outputFormat

Output a single line containing the aggregated result as a JSON object. The keys must be in lexicographical order, and the format should have no extra spaces.

For example:

{"apple":6,"banana":3,"orange":1}
## sample
4
apple 2
banana 3
apple 4
orange 1
{"apple":6,"banana":3,"orange":1}