#K80262. Update Inventory Based on Transactions
Update Inventory Based on Transactions
Update Inventory Based on Transactions
You are given an inventory list with n items and a series of m transactions. Each transaction consists of an item index and a transaction amount which can be either positive (for restocking) or negative (for selling). For each transaction, if applying the transaction won’t cause the item's quantity to fall below zero, then update the inventory accordingly. Formally, for an item i and transaction amount d, the update is applied only if $$inventory_i + d \ge 0$$. Your task is to process all transactions and output the updated inventory.
Note: All transactions are applied in the order they are given.
inputFormat
The input is read from standard input and has the following format:
- An integer n representing the number of items in the inventory.
- A line with n space-separated integers representing the initial quantities of each item.
- An integer m representing the number of transactions.
- m lines each containing two integers: the item index (0-indexed) and the transaction amount.
A transaction is applied only if it maintains non-negative inventory for that item.
Mathematically, for a transaction on item i with adjustment d, the update is performed if and only if $$inventory_i + d \ge 0$$.
outputFormat
Output a single line to standard output containing n space-separated integers which represent the updated inventory after all valid transactions have been processed.
## sample4
10 5 8 3
5
0 -3
1 7
2 -10
3 -1
0 1
8 12 8 2