#K36807. Calculate Total Fruit Weight
Calculate Total Fruit Weight
Calculate Total Fruit Weight
You are given a list of fruits with their individual weights. Your task is to compute the total weight for each distinct fruit. Each input element consists of a fruit name and a weight. The result should be output as a JSON object where the keys are the fruit names and the values are their corresponding total weights.
For clarity, if a fruit appears multiple times in the input, you must sum all of its weights. The keys in the output JSON must be sorted in lexicographical order.
Hint: Use an appropriate data structure (like a dictionary or map) to accumulate the weights. If needed, you can refer to the formula \( T_f = \sum_{i=1}^{n_f} w_{i} \), where \( T_f \) is the total weight for fruit \( f \), and \( w_{i} \) is the weight of its \( i \)th occurrence.
inputFormat
The input is provided via standard input (stdin) with the following format:
- The first line contains an integer \( N \) representing the number of fruit entries.
- The next \( N \) lines each contain a fruit name (a string) and its weight (an integer) separated by a space.
outputFormat
Output a JSON object (to standard output) representing the total weight of each fruit. The keys of the JSON object should be the fruit names and must appear in lexicographical order, and the values should be the corresponding total weights.
For example, if the computed totals are:
{"apple": 320, "banana": 270, "orange": 130}## sample
5
apple 150
banana 120
apple 170
orange 130
banana 150
{"apple": 320, "banana": 270, "orange": 130}