Order book Yuriy Lyfenko

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Back to listApr 2, 2025 04:19Yuriy LyfenkoYuriy LyfenkoScore: 46,366Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 2, 2025 04:2041,26751,807,68022,895,0001,040,0002,347,008
2Apr 2, 2025 04:2041,93149,618,91423,307,0001,013,0002,347,008
3Apr 2, 2025 04:2143,09546,694,72123,996,000999,0002,334,720
4Apr 2, 2025 04:2143,21747,196,12323,061,0002,005,0002,342,912
5Apr 2, 2025 04:1943,45246,048,63524,194,0001,008,0002,420,736
6Apr 2, 2025 04:2044,12844,717,08925,594,00002,498,560
7Apr 2, 2025 04:2144,41654,621,31724,731,0001,030,0002,469,888
8Apr 2, 2025 04:2045,68451,412,18725,478,0001,019,0002,301,952
9Apr 2, 2025 04:2045,83152,407,69026,582,00002,490,368
10Apr 2, 2025 04:2145,84549,631,93726,590,00002,506,752
11Apr 2, 2025 04:2146,11953,694,34025,721,0001,028,0002,502,656
12Apr 2, 2025 04:2146,15951,119,30725,743,0001,029,0002,494,464
13Apr 2, 2025 04:2046,36658,973,86225,896,000996,0002,457,600
14Apr 2, 2025 04:2146,37151,403,03924,903,0001,992,0002,457,600
15Apr 2, 2025 04:2148,29151,860,36927,009,0001,000,0002,469,888
16Apr 2, 2025 04:2048,94350,825,29327,374,0001,013,0002,424,832
17Apr 2, 2025 04:1949,82655,214,29926,906,0001,993,0002,367,488
18Apr 2, 2025 04:2149,88353,555,20327,935,000997,0002,437,120
19Apr 2, 2025 04:2150,77252,700,66228,433,0001,015,0002,490,368
20Apr 2, 2025 04:2050,82848,906,48429,480,00002,347,008
21Apr 2, 2025 04:2151,11755,719,91529,648,00002,498,560
22Apr 2, 2025 04:2051,53856,621,47527,900,0001,992,0002,510,848
23Apr 2, 2025 04:2151,73153,699,49728,004,0002,000,0002,387,968
24Apr 2, 2025 04:1955,85562,979,78732,396,00002,351,104