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 14:46Joad 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 24, 2024 19:4300000Error: expected "1140171 ", got "1023395 "stderr
2Nov 24, 2024 19:4300000Error: expected "2022885 ", got "1989296 "stderr
3Nov 24, 2024 19:4200000Error: expected "1122637 ", got "1108049 "stderr
4Nov 23, 2024 16:5900000Error: expected "1614145 ", got "1397000 "stderr
5Nov 23, 2024 16:4100000Error: expected "1007204 ", got "1002544 "stderr
6Nov 23, 2024 14:4600000Error: expected "1320814 ", got "1249000 "stderr
7Nov 23, 2024 14:4600000Error: expected "2542248 ", got "2344000 "stderr
8Nov 23, 2024 16:4134,91940,548,14919,241,0001,012,0002,363,392
9Nov 23, 2024 16:4035,29350,371,68319,447,0001,023,0002,424,832
10Nov 23, 2024 16:4135,79845,255,53320,763,00002,441,216
11Nov 24, 2024 19:4336,04550,614,61219,911,000995,0002,408,448
12Nov 23, 2024 17:4736,22240,563,27320,009,0001,000,0002,461,696
13Nov 23, 2024 17:4736,24348,174,35519,019,0002,002,0002,387,968
14Nov 23, 2024 16:4036,43647,418,46419,121,0002,012,0002,420,736
15Nov 23, 2024 16:4036,62638,614,37920,232,0001,011,0002,449,408
16Nov 23, 2024 17:4736,88848,988,56321,395,00002,424,832
17Nov 23, 2024 16:4137,02945,400,74221,477,00002,465,792
18Nov 24, 2024 19:4337,22843,685,68320,564,0001,028,0002,449,408
19Nov 24, 2024 19:4337,44539,123,16320,684,0001,034,0002,490,368