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 listMar 2, 2026 21:26JuneLJuneLScore: 73,224Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 2, 2026 21:2667,37864,117,76533,067,0006,012,0002,371,584
2Mar 2, 2026 21:1367,48172,192,12233,118,0006,021,0002,461,696
3Mar 2, 2026 21:1368,13367,003,48633,438,0006,079,0002,338,816
4Mar 2, 2026 21:2668,17167,337,11635,484,0004,055,0002,314,240
5Mar 2, 2026 21:2668,32165,855,72833,530,0006,096,0002,453,504
6Mar 2, 2026 21:2668,38469,772,36034,578,0005,085,0002,347,008
7Mar 2, 2026 21:1369,34866,374,78436,200,0004,022,0002,375,680
8Mar 2, 2026 21:2669,54870,155,88536,305,0004,033,0002,424,832
9Mar 2, 2026 21:1370,12468,632,36134,572,0006,100,0002,531,328
10Mar 2, 2026 21:1370,48467,826,79233,902,0006,979,0002,482,176
11Mar 2, 2026 21:1271,10366,682,53136,211,0005,029,0002,347,008
12Mar 2, 2026 21:1371,36666,767,85636,345,0005,047,0002,314,240
13Mar 2, 2026 21:1271,42866,378,10836,376,0005,052,0002,412,544
14Mar 2, 2026 21:2671,52166,673,87635,412,0006,070,0002,371,584
15Mar 2, 2026 21:1371,65568,943,74935,478,0006,082,0002,502,656
16Mar 2, 2026 21:2672,56770,476,99437,079,0005,010,0002,502,656
17Mar 2, 2026 21:1273,22476,814,98036,403,0006,067,0002,408,448
18Mar 2, 2026 21:2673,34370,525,46737,475,0005,064,0002,478,080
19Mar 2, 2026 21:1274,09871,857,09336,981,0005,996,0002,387,968
20Mar 2, 2026 21:1374,26970,783,76938,068,0005,008,0002,514,944
21Mar 2, 2026 21:1274,39776,043,75337,129,0006,021,0002,449,408
22Mar 2, 2026 21:2674,51669,430,88437,189,0006,030,0002,330,624
23Mar 2, 2026 21:1275,08367,260,49538,485,0005,063,0002,355,200
24Mar 2, 2026 21:2675,18869,307,90037,524,0006,085,0002,510,848
25Mar 2, 2026 21:2675,79072,260,97837,964,0005,994,0002,342,912
26Mar 2, 2026 21:1276,39369,378,86638,266,0006,042,0002,396,160
27Mar 2, 2026 21:2677,41478,692,56538,914,0005,986,0002,301,952
28Mar 2, 2026 21:1278,17879,059,45139,298,0006,045,0002,318,336
29Mar 2, 2026 21:1279,11269,943,63937,905,0007,980,0002,387,968
30Mar 2, 2026 21:2679,66974,297,76140,181,0006,027,0002,293,760
31Mar 2, 2026 21:2680,99875,609,63740,982,0005,997,0002,478,080
32Mar 2, 2026 21:1383,80375,792,10542,531,0006,075,0002,473,984
33Mar 2, 2026 21:2686,23374,406,06845,014,0005,001,0002,502,656