#C7449. Group Coins by Year
Group Coins by Year
Group Coins by Year
You are given a list of coins, each represented by two integers: a coin's value and the year it was minted.
Your task is to group the coins by their minting year and then calculate the total value of coins for each year. The output must list the years in ascending order along with their corresponding total coin value.
For example, if the input is:
5 100 1995 50 1995 25 1996 75 1996 30 1997
Then the output should be:
1995 150 1996 100 1997 30
Note: The problem can be formally described as: Given a list of pairs \( (v_i, y_i) \), compute for each unique year \( y \) the sum \( S_y = \sum_{i: y_i=y} v_i \). Ensure that the result is printed with the years ordered in ascending order.
inputFormat
The first line contains an integer n representing the number of coins. The following n lines each contain two integers separated by a space: the coin's value and its minting year.
Input format:
n value1 year1 value2 year2 ... valuen yearn
outputFormat
For each unique year (in ascending order), output a line containing the year and the total value of coins minted in that year, separated by a space.
Output format:
year1 total_value1 year2 total_value2 ...## sample
1
100 1995
1995 100
</p>