Under construction

Still being built. Things may change

Stock Market

Time limit1sMemory limit1024 MB

Summary
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 pip_i.
    • If the sell volume sis_i at price pip_i is at least 1 and you want to buy xix_i shares, then min⁡(si,xi)\min(s_i, x_i) shares are bought. If xi>six_i > s_i, the remaining xi−min⁡(si,xi)x_i - \min(s_i, x_i) shares become the buy volume at that price. The sell volume sis_i decreases by the number of shares bought.
    • If the buy volume sis_i at price pip_i is at least 1 and you want to sell xix_i shares, then min⁡(si,xi)\min(s_i, x_i) shares are sold. If xi>six_i > s_i, the remaining xi−min⁡(si,xi)x_i - \min(s_i, x_i) shares become the sell volume at that price. The buy volume sis_i 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 pip_i, the price changes to pip_i.
    • If no share can be bought, the price stays the same, and xix_i shares are added to the buy volume at price pip_i.
    • If no share can be sold, the price stays the same, and xix_i shares are added to the sell volume at price pip_i.

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 NN. (1≤N≤100 0001 \leq N \leq 100\,000)

Each of the next NN lines contains one order (pi,xi,fi)(p_i, x_i, f_i) as pip_i, xix_i, and fif_i, separated by spaces. (7000≤pi≤130007000 \leq p_i \leq 13000; pip_i is a multiple of 10; 1≤xi≤10001 \leq x_i \leq 1000; fif_i is −1-1 or 11)

fif_i is −1-1 for a sell order and 11 for a buy order. All given numbers are integers.

Output

Print the current price of the HRX stock after all NN orders are processed.

Examples2

  1. Example 1

    Input
    6
    10000 1 -1
    10010 1 1
    10010 1 -1
    10030 100 -1
    10030 101 1
    10070 100 -1
    
    Expected output
    10030
    
  2. Example 2

    Input
    3
    7000 1000 -1
    13000 1000 1
    10000 1000 1
    
    Expected output
    10000