#C6679. Aggregate Values by Key

    ID: 50465 Type: Default 1000ms 256MiB

Aggregate Values by Key

Aggregate Values by Key

You are given a list of dictionary entries where each entry contains a category and an integer value. In addition, you are provided a key string, which indicates the field of aggregation. For each entry, the first token represents the category (which corresponds to the given key) and the second token is an integer value. Your task is to aggregate the values for each unique category and output the result. The output should list each unique category alongside the summed value, with the lines sorted lexicographically by the category.

In other words, if the input key is K and you have N entries, each entry is in the format:

<category> <value>

Then for each distinct category, compute ( S = \sum_{i \text{ with } category} value_i ) and print each category and its sum on a separate line, sorted by the category in ascending order.

inputFormat

The first line contains a string K, which is the key to aggregate by (for example, 'category'). The second line contains an integer N, the number of entries. Each of the following N lines contains a category (a string) and a value (an integer) separated by a space.

outputFormat

Output the aggregated result for each unique category in ascending lexicographical order. For each category, print a line with the category, a space, and the corresponding aggregated sum.## sample

category
5
A 10
B 20
A 5
C 7
B 3
A 15

B 23 C 7

</p>