#C7195. Stock Inventory Update
Stock Inventory Update
Stock Inventory Update
You are required to update the inventory stock levels for a number of products based on a list of transactions. The system supports two operations:
- R: Restock operation. The product's stock is increased by the given quantity.
- P: Purchase operation. If there is enough stock, the product's stock decreases by the given quantity; otherwise, the stock is set to 0 and the output for that transaction should be "0 Insufficient stock".
For each transaction, print the current stock level (or the error message) on a separate line.
More formally, let \(S_i\) be the stock of product \(i\). For a restock transaction on product \(i\) with quantity \(q\), update \(S_i \leftarrow S_i + q\) and output \(S_i\). For a purchase transaction, if \(S_i < q\), then set \(S_i = 0\) and output "0 Insufficient stock"; otherwise, update \(S_i \leftarrow S_i - q\) and output the new \(S_i\).
inputFormat
The input is read from standard input (stdin) and has the following format:
n s1 s2 ... sn m op1 id1 q1 op2 id2 q2 ... op m id m q m
- n: an integer representing the number of product types.
- The second line contains n integers representing the initial stock for each product (product 1 to product n).
- m: an integer representing the number of transactions.
- The next m lines each contain a transaction in the format:
operation product_id quantity
- operation is a character: 'R' for restock or 'P' for purchase.
- product_id is 1-indexed.
- quantity is an integer.
- If the operation is 'R', output the updated stock of the product.
- If the operation is 'P' and there is sufficient stock, output the updated stock after subtracting the quantity; otherwise, output "0 Insufficient stock".
outputFormat
For each transaction, output a single line on standard output (stdout):
5
10 20 30 40 50
6
R 3 15
P 4 45
P 2 10
R 1 5
P 5 60
P 1 20
45
0 Insufficient stock
10
15
0 Insufficient stock
0 Insufficient stock
</p>