#C1540. Inventory Update
Inventory Update
Inventory Update
You are given the initial inventory of books and a list of transactions. Each inventory entry consists of a book title and the number of copies available. Each transaction consists of a book title and a change in the number of copies (a positive number indicates restocking and a negative number indicates sales). After applying all transactions sequentially, any book with a non-positive number of copies is removed from the inventory.
Your task is to update the inventory accordingly and output the final list of books with their available copies in the order of their first appearance. If a book is re-added after being removed, consider it as a new entry.
In mathematical terms, if the initial inventory is given by \(I = \{(t_i, c_i)\}_{i=1}^{n}\) and the transactions are \(T = \{(t_j, \delta_j)\}_{j=1}^{m}\), then for each transaction, update the count \(c\) for the corresponding title \(t\) as:
[ c' = c + \delta, \quad \text{if } c + \delta > 0, \quad \text{otherwise remove } t ]
Output the remaining books in the order they were first added (or re-added).
inputFormat
The first line contains an integer ( n ), the number of books in the initial inventory. Each of the next ( n ) lines contains a book title and an integer representing the initial number of copies. Note that the book title may include spaces, and the integer is the last token on the line. Next, a line containing an integer ( m ) is given, representing the number of transactions. Each of the following ( m ) lines contains a book title (which may contain spaces) and an integer indicating the change in the number of copies.
outputFormat
For each book in the updated inventory (with a positive number of copies), output a single line containing the book title followed by the final number of copies, separated by a space. If no book remains, output nothing.## sample
3
To Kill a Mockingbird 3
1984 5
The Great Gatsby 2
4
1984 -3
To Kill a Mockingbird -1
Harry Potter 5
The Great Gatsby -2
To Kill a Mockingbird 2
1984 2
Harry Potter 5
</p>