#C385. Top N Users by Transaction Sum

    ID: 47322 Type: Default 1000ms 256MiB

Top N Users by Transaction Sum

Top N Users by Transaction Sum

You are given a series of transaction logs. Each log is a single line that consists of a user ID and a transaction value separated by a comma. Your task is to calculate the total transaction value for each user and then output the IDs of the top N users with the highest cumulative transaction values. In the event of a tie (i.e. two users have the same total), the user with the smaller user ID should come first.

The transaction accumulation can be mathematically formulated as follows:

$$ S_u = \sum_{i=1}^{M_u} v_{i} $$

where \(S_u\) is the sum of transaction values for user \(u\), \(M_u\) is the number of transactions for user \(u\), and \(v_{i}\) is the value of the \(i^{th}\) transaction.

Implement a solution that reads input from standard input (stdin) and writes the answer to standard output (stdout).

inputFormat

The first line contains two integers M and N, where M is the number of log entries and N is the number of top users to output. The following M lines each contain a log entry in the format userID,transactionValue.

Example:

6 2
123,100
34,200
56,150
34,120
123,130
56,180

outputFormat

Output a single line containing the top N user IDs separated by a space, ordered from the highest cumulative transaction value to the lowest. If there is a tie, the user with the smaller user ID appears first.

Example:

56 34
## sample
6 2
123,100
34,200
56,150
34,120
123,130
56,180
56 34

</p>