Stock Market
Time limit1sMemory limit1024 MB
Given N buy and sell orders on a virtual stock market, print the HRX price after all orders are processed.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Never lose the first rule. The second rule is to always follow the first.
- Warren Buffett
Haru has recently been hooked on the stock market. Haru studied how stock prices move and built a virtual stock market based on that research.
The virtual stock market runs under the following rules.
- A share is one unit, and prices change in steps of 10 won.
- The price always starts at 10000 won. At first, every price in steps of 10 won has zero shares in both its buy volume and its sell volume.
- To buy or sell a stock, place an order at price .
- If the sell volume at price is at least 1 and you want to buy shares, then shares are bought. If , the remaining shares become the buy volume at that price. The sell volume decreases by the number of shares bought.
- If the buy volume at price is at least 1 and you want to sell shares, then shares are sold. If , the remaining shares become the sell volume at that price. The buy volume decreases by the number of shares sold.
- The price changes according to these conditions.
- When an order arrives and at least 1 share can be bought or sold at price , the price changes to .
- If no share can be bought, the price stays the same, and shares are added to the buy volume at price .
- If no share can be sold, the price stays the same, and shares are added to the sell volume at price .
The figure below shows the case where the current price is 10000 won and an order to buy 250 shares is placed at 10020 won. The current price becomes 10020 won, and the 200 shares of sell volume at 10020 won are converted into 50 shares of buy volume.

In this virtual market, Haru can trade the stock of HRX, the company Haru created.
Input
The first line contains the number of orders . ()
Each of the next lines contains one order as , , and , separated by spaces. (; is a multiple of 10; ; is or )
is for a sell order and for a buy order. All given numbers are integers.
Output
Print the current price of the HRX stock after all orders are processed.