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 21, 2025 19:04Josu San MartinJosu San MartinScore: 36,734Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 19:0435,62849,148,78719,631,0001,033,0002,453,504
2Dec 21, 2025 19:0735,82945,284,87220,781,00002,465,792
3Dec 21, 2025 19:0436,02949,269,65417,912,0002,985,0002,490,368
4Dec 21, 2025 19:0436,04355,499,42419,910,000995,0002,506,752
5Dec 21, 2025 19:0736,10343,966,45819,893,0001,047,0002,490,368
6Dec 21, 2025 19:0436,13449,630,99119,960,000998,0002,408,448
7Dec 22, 2025 01:4636,31453,361,86821,062,00002,424,832
8Dec 21, 2025 19:0436,31752,402,86419,058,0002,006,0002,465,792
9Dec 21, 2025 19:0436,71041,101,45220,279,0001,013,0002,387,968
10Dec 21, 2025 19:0736,73445,049,93520,292,0001,014,0002,465,792
11Dec 21, 2025 19:0436,77244,326,06721,328,00002,342,912
12Dec 22, 2025 01:4636,95349,232,04320,413,0001,020,0002,322,432
13Dec 21, 2025 19:0436,97443,638,34720,424,0001,021,0002,478,080
14Dec 21, 2025 19:0437,05741,919,15421,493,00002,363,392
15Dec 21, 2025 19:0437,08644,263,74220,486,0001,024,0002,473,984
16Dec 21, 2025 19:0437,12844,505,37721,534,00002,293,760
17Dec 21, 2025 19:0437,20242,381,67520,550,0001,027,0002,269,184
18Dec 22, 2025 01:4637,40553,144,20020,662,0001,033,0002,326,528