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 listNov 23, 2024 03:38Joad NacerJoad NacerError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 23, 2024 03:3800000Error: expected "1177995 ", got "1175923 "stderr
2Nov 23, 2024 14:3900000Error: expected "1305696 ", got "1286758 "stderr
3Nov 23, 2024 03:4000000Error: expected "960981 ", got "946024 "stderr
4Nov 23, 2024 03:3900000Error: expected "2138883 ", got "2032000 "stderr
5Nov 23, 2024 03:3900000Error: expected "2093781 ", got "1558517 "stderr
6Nov 23, 2024 03:3900000Error: expected "1212007 ", got "1148000 "stderr
7Nov 23, 2024 03:3900000Error: expected "2941798 ", got "2820000 "stderr
8Nov 23, 2024 03:3900000Error: expected "1969485 ", got "1931005 "stderr
9Nov 23, 2024 03:3800000Error: No answer to stdout. Died or killed?stderr
10Nov 23, 2024 03:3935,35751,964,70719,482,0001,025,0002,437,120
11Nov 23, 2024 03:3935,40541,411,31919,509,0001,026,0002,457,600
12Nov 23, 2024 03:3936,10741,731,50118,948,0001,994,0002,433,024
13Nov 23, 2024 03:3936,15750,250,01319,973,000998,0002,494,464
14Nov 23, 2024 03:3936,32247,409,68220,064,0001,003,0002,392,064
15Nov 23, 2024 03:3936,35547,924,03721,086,00002,469,888
16Nov 23, 2024 03:3936,59348,610,26219,203,0002,021,0002,502,656
17Nov 23, 2024 03:3937,25047,376,97721,605,00002,482,176
18Nov 23, 2024 03:3937,82446,142,83319,944,0001,994,0002,428,928