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 listDec 31, 2024 18:18Louis PonetLouis PonetError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 31, 2024 18:1800000Error: No answer to stdout. Died or killed?stderr
2Dec 31, 2024 18:1900000Error: No answer to stdout. Died or killed?stderr
3Dec 31, 2024 18:1900000Error: No answer to stdout. Died or killed?
4Dec 31, 2024 18:1900000Error: No answer to stdout. Died or killed?stderr
5Dec 31, 2024 18:1996,76682,580,10655,122,0001,002,0002,293,760
6Dec 31, 2024 18:1996,83177,200,64954,157,0002,005,0002,285,568
7Dec 31, 2024 18:1896,90082,988,58754,195,0002,007,0002,289,664
8Dec 31, 2024 18:1997,15782,324,51955,345,0001,006,0002,285,568
9Dec 31, 2024 18:1897,43880,809,17655,505,0001,009,0002,297,856
10Dec 31, 2024 18:1998,00383,277,74854,848,0001,994,0002,297,856
11Dec 31, 2024 18:1998,06288,665,67355,879,000997,0002,392,064
12Dec 31, 2024 18:1998,38179,817,58155,059,0002,002,0002,289,664
13Dec 31, 2024 18:1898,38883,038,92455,063,0002,002,0002,269,184
14Dec 31, 2024 18:1998,93682,038,73156,377,0001,006,0002,293,760
15Dec 31, 2024 18:1899,15380,129,62757,509,00002,269,184
16Dec 31, 2024 18:1999,25782,552,24257,569,00002,269,184
17Dec 31, 2024 18:1899,80091,822,82355,888,0001,996,0002,273,280
18Dec 31, 2024 18:1899,83183,029,03556,904,000998,0002,273,280
19Dec 31, 2024 18:1999,91788,938,37055,954,0001,998,0002,269,184
20Dec 31, 2024 18:18100,70782,600,49058,410,00002,277,376
21Dec 31, 2024 18:19101,24186,094,02158,720,00002,293,760
22Dec 31, 2024 18:18101,54187,723,91657,896,000998,0002,293,760
23Dec 31, 2024 18:18102,51986,293,85059,461,00002,289,664
24Dec 31, 2024 18:18102,69583,129,31459,563,00002,297,856
25Dec 31, 2024 18:18103,22686,884,99059,871,00002,297,856
26Dec 31, 2024 18:18103,25989,784,91857,894,0001,996,0002,289,664
27Dec 31, 2024 18:19104,32994,124,01358,494,0002,017,0002,277,376
28Dec 31, 2024 18:19104,47281,406,06960,594,00002,273,280