Two warehouses each keep a stock ledger: a list of entries (sku, count) giving the number of units on hand for a SKU code. Within a single ledger every SKU code is distinct and the entries are already listed in strictly increasing order of SKU code.
You must produce one consolidated ledger covering both warehouses: for a SKU that appears in only one of the two ledgers, its count in the consolidated ledger is exactly that count; for a SKU that appears in both ledgers, its count in the consolidated ledger is the sum of the two counts. The consolidated ledger must list every distinct SKU exactly once, sorted in strictly increasing order of SKU code.
The first line contains an integer n, the number of entries in the first warehouse's ledger. Each of the next n lines contains two integers sku and count for that ledger, given in strictly increasing order of sku. The next line contains an integer m, the number of entries in the second warehouse's ledger, followed by m lines of sku and count in the same format, also strictly increasing by sku.
Print an integer k, the number of distinct SKUs in the consolidated ledger, on its own line. Then print k lines, each containing a SKU code and its consolidated count, sorted in strictly increasing order of SKU code.
Example 1
Input
3 10 5 20 3 50 7 3 10 2 30 4 50 1
Expected
4 10 7 20 3 30 4 50 8
Explanation
Warehouse A has SKUs 10(5),20(3),50(7); warehouse B has SKUs 10(2),30(4),50(1). SKU 10 appears in both: 5+2=7. SKU 20 only in A: 3. SKU 30 only in B: 4. SKU 50 in both: 7+1=8. Sorted by SKU the consolidated ledger is 4 entries: (10,7),(20,3),(30,4),(50,8).
Example 2
Input
2 5 9 8 2 3 3 6 8 5 12 1
Expected
4 3 6 5 9 8 7 12 1
Explanation
Warehouse A has SKUs 5(9),8(2); warehouse B has SKUs 3(6),8(5),12(1). SKU 3 only in B: 6. SKU 5 only in A: 9. SKU 8 in both: 2+5=7. SKU 12 only in B: 1. Sorted by SKU the consolidated ledger is 4 entries: (3,6),(5,9),(8,7),(12,1).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →