#K4046. Comparing Monthly Product Sales
Comparing Monthly Product Sales
Comparing Monthly Product Sales
You are given the daily sales data for products over two different months. Each month’s data consists of several entries in the format product_id,sales
, indicating the product's identifier and the number of sales for that day. The first line of the input provides two integers n and m representing the number of records for month 1 and month 2, respectively.
Your task is to calculate the total sales for each product in each month, compute the absolute difference of sales between the two months for each product, and then output the results sorted in ascending order of product_id.
The calculation can be mathematically represented as follows:
\[ \text{difference}(p) = \left| \sum_{i=1}^{n_p} sales_{1,i} - \sum_{j=1}^{m_p} sales_{2,j} \right| \]
where \( n_p \) and \( m_p \) are the number of records for product \( p \) in month 1 and month 2 respectively.
Example:
Input: 3 3 101,300 102,250 103,500 101,400 102,300 103,450</p>Output: 101 100 102 50 103 50
inputFormat
The input is read from stdin and has the following format:
- The first line contains two space-separated integers n and m, representing the number of sales records for month 1 and month 2 respectively.
- The next n lines each contain a record for month 1 in the format
product_id,sales
. - The following m lines each contain a record for month 2 in the format
product_id,sales
.
outputFormat
Output the absolute difference in total sales for each product across the two months to stdout. For each product, print a line with two space-separated integers: the product_id and the absolute difference in sales. The output should be sorted in ascending order by product_id.
## sample3 3
101,300
102,250
103,500
101,400
102,300
103,450
101 100
102 50
103 50
</p>