#C8360. Shopping Cart Manager
Shopping Cart Manager
Shopping Cart Manager
You are required to implement a simple shopping cart manager. The cart supports three operations:
- add — Add an item to the cart with a unique identifier and a price.
- remove — Remove an item from the cart by its identifier.
- total — Compute the total price of all items currently in the cart.
When computing the total price, use the formula: \(T = \sum_{i=1}^{n} price_i \), where \(price_i\) is the price of the \(i^{th}\) item in the cart.
The input will specify a sequence of operations. The operations are processed sequentially and exactly one "total" command will be issued that requests the final total price to be printed.
inputFormat
The first line of input contains an integer \(N\) representing the number of operations. The following \(N\) lines each contain an operation.
An operation is in one of the following formats:
add item_id price
— add an item with identifieritem_id
and costprice
.remove item_id
— remove the item with identifieritem_id
if it exists.total
— output the sum of prices of all items currently in the cart.
Each item_id and price is an integer. It is guaranteed that the total
operation appears exactly once and is the final command.
outputFormat
Output a single integer which is the total price of all items in the cart after processing all operations.
## sample3
add 101 20
add 102 40
total
60