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 30, 2025 13:47etherealetherealScore: 113,619Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 30, 2025 13:47112,83890,698,94760,412,0005,034,0002,330,624
2Apr 30, 2025 13:41112,88494,545,36060,437,0005,036,0002,416,640
3Apr 30, 2025 13:41113,01689,361,37556,473,0009,076,0002,318,336
4Apr 30, 2025 13:41113,08492,244,03558,526,0007,063,0002,437,120
5Apr 30, 2025 13:47113,15590,968,50759,572,0006,058,0002,412,544
6Apr 30, 2025 13:41113,26988,528,78462,664,0003,032,0002,412,544
7Apr 30, 2025 13:47113,52991,420,67158,864,0006,983,0002,428,928
8Apr 30, 2025 13:47113,56987,369,05858,884,0006,986,0002,367,488
9Apr 30, 2025 13:41113,60094,625,14858,900,0006,988,0002,347,008
10Apr 30, 2025 13:41113,61989,957,47559,909,0005,990,0002,342,912
11Apr 30, 2025 13:41113,62687,991,29459,912,0005,991,0002,531,328
12Apr 30, 2025 13:47113,76693,981,94055,987,0009,997,0002,367,488
13Apr 30, 2025 13:41113,96493,486,93561,092,0005,007,0002,408,448
14Apr 30, 2025 13:47114,27893,184,23360,256,0006,025,0002,383,872
15Apr 30, 2025 13:47114,30595,580,67163,284,0003,013,0002,404,352
16Apr 30, 2025 13:47114,31990,736,98659,273,0007,032,0002,392,064
17Apr 30, 2025 13:47114,91991,644,20260,594,0006,059,0002,359,296
18Apr 30, 2025 13:41115,25791,533,77454,876,00011,973,0002,387,968