#C238. Inventory Management
Inventory Management
Inventory Management
Inventory Management
You are given a series of operations to manage an inventory. Each operation is either adding items to the inventory or removing items. For an ADD
operation, add the specified quantity to the item (if it already exists, increase its count; otherwise, create a new entry). For a REMOVE
operation, decrease the quantity for that item, but ensure that the quantity never becomes negative. If a REMOVE
operation tries to remove more items than available, the quantity should be set to 0.
After processing all operations, output the final inventory sorted in lexicographical order by item name. Each line of the output should contain the item name followed by its quantity, separated by a space.
Input and output are handled via standard input (stdin) and standard output (stdout).
inputFormat
The first line of input contains a single integer n, representing the number of operations. The following n lines each contain an operation in the format:
OPERATION ITEM QUANTITY
where OPERATION
is either ADD
or REMOVE
, ITEM
is a string representing the item name, and QUANTITY
is an integer.
outputFormat
Print the final inventory with each item on a separate line. Each line should contain the item name and its quantity separated by a space. The items must be output in lexicographical (alphabetical) order.## sample
3
ADD apple 10
REMOVE apple 3
ADD banana 5
apple 7
banana 5
</p>