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 listSep 27, 2025 19:36Wookyung JeongWookyung JeongScore: 61,533Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 19:3558,53156,621,16026,959,0006,989,0002,441,216
2Sep 27, 2025 19:3559,01762,050,69231,210,0003,020,0002,441,216
3Sep 27, 2025 19:3559,93459,784,63226,817,0007,945,0002,445,312
4Sep 27, 2025 19:3560,14864,092,57223,922,00010,964,0002,494,464
5Sep 27, 2025 19:3660,28156,656,30929,969,0004,994,0002,306,048
6Sep 27, 2025 19:3560,54561,621,31529,096,0006,020,0002,465,792
7Sep 27, 2025 19:3660,69559,169,63330,174,0005,029,0002,334,720
8Sep 27, 2025 19:3560,71055,497,43331,188,0004,024,0002,297,856
9Sep 27, 2025 19:3560,85558,608,50329,246,0006,050,0002,428,928
10Sep 27, 2025 19:3561,01260,892,60228,310,0007,077,0002,297,856
11Sep 27, 2025 19:3561,04556,964,65132,372,0003,034,0002,404,352
12Sep 27, 2025 19:3561,31260,499,26128,449,0007,112,0002,433,024
13Sep 27, 2025 19:3561,47165,423,21631,579,0004,074,0002,314,240
14Sep 27, 2025 19:3561,53361,002,49833,650,0002,039,0002,334,720
15Sep 27, 2025 19:3561,86962,786,34529,904,0005,980,0002,330,624
16Sep 27, 2025 19:3562,01667,068,47832,972,0002,997,0002,502,656
17Sep 27, 2025 19:3562,16458,726,81830,046,0006,009,0002,424,832
18Sep 27, 2025 19:3662,23359,395,67531,082,0005,013,0002,387,968
19Sep 27, 2025 19:3562,56764,199,85930,241,0006,048,0002,412,544
20Sep 27, 2025 19:3563,22462,523,95331,577,0005,093,0002,473,984
21Sep 27, 2025 19:3663,23466,575,16832,601,0004,075,0002,310,144
22Sep 27, 2025 19:3663,59761,761,74028,911,0007,975,0002,510,848
23Sep 27, 2025 19:3564,02464,778,07231,113,0006,021,0002,351,104
24Sep 27, 2025 19:3564,33861,970,39035,299,0002,017,0002,273,280
25Sep 27, 2025 19:3564,35960,733,59233,293,0004,035,0002,502,656
26Sep 27, 2025 19:3566,35264,652,30332,408,0006,076,0002,347,008
27Sep 27, 2025 19:3666,69763,614,81134,612,0004,072,0002,437,120