#C8160. Shopping Cart Simulation

    ID: 52112 Type: Default 1000ms 256MiB

Shopping Cart Simulation

Shopping Cart Simulation

In this problem, you will simulate a shopping cart system that supports operations for adding items, removing items, applying discount coupons, and calculating the final total cost. The computation of the total cost is defined by the formula:

[ \text{Total} = \max\left(0, \sum_{i}\left(price_i \times quantity_i\right) - \Bigl(\sum_{j; \text{fixed}} value_j + \sum_{k; \text{percent}} \frac{value_k}{100} \times \sum_{i}\left(price_i \times quantity_i\right)\Bigr)\right)]

The available operations are as follows:

  1. ADD: Adds an item with a given price and quantity. If the item already exists, its quantity is increased.
  2. REMOVE: Removes a specified quantity of an item. If the remaining quantity is zero or less, the item is removed from the cart.
  3. COUPON: Applies a discount coupon. Coupons can be of type fixed (subtracting a fixed amount) or percent (subtracting a percentage of the total cost).
  4. TOTAL: Calculates and outputs the final total cost, after applying all operations. The command TOTAL terminates the input sequence.

Your task is to process a sequence of these operations and print the final total cost to stdout. The final total cost should not be negative.

inputFormat

The input starts with an integer N on a new line, representing the number of operations. The following N lines each contain one operation in one of the following formats:

  • ADD item_name price quantity
  • REMOVE item_name quantity
  • COUPON coupon_code coupon_type coupon_value
  • TOTAL

It is guaranteed that there will be exactly one TOTAL command, which terminates the processing of operations.

outputFormat

Output a single integer representing the final total cost after applying the discounts. Note that the total cost is never negative.## sample

3
ADD Laptop 1000 1
COUPON DISCOUNT100 fixed 100
TOTAL
900

</p>