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 19, 2026 10:48Dominique GarmierDominique GarmierScore: 18,495Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 08:1018,25328,162,7339,529,0001,058,0002,289,664
2Mar 20, 2026 08:1018,31426,224,3009,560,0001,062,0002,519,040
3Mar 20, 2026 07:4918,31731,654,98710,624,00002,289,664
4Mar 20, 2026 07:4918,32130,318,05310,626,00002,289,664
5Mar 19, 2026 03:2818,36931,066,1259,589,0001,065,0002,506,752
6Mar 21, 2026 02:0318,38129,243,78410,661,00002,285,568
7Mar 21, 2026 02:5118,38331,413,8459,596,0001,066,0002,289,664
8Mar 21, 2026 01:0518,38332,102,1169,596,0001,066,0002,289,664
9Mar 21, 2026 02:5118,39726,993,8639,603,0001,067,0002,289,664
10Mar 21, 2026 02:5818,41027,547,6499,611,0001,067,0002,289,664
11Mar 21, 2026 01:0518,41027,835,2569,611,0001,067,0002,289,664
12Mar 21, 2026 02:5818,41628,423,0989,613,0001,068,0002,506,752
13Mar 20, 2026 08:1018,42127,208,1849,616,0001,068,0002,289,664
14Mar 19, 2026 03:2818,42933,619,20310,689,00002,510,848
15Mar 15, 2026 21:3118,43326,512,4089,622,0001,069,0002,289,664
16Mar 19, 2026 03:2818,43426,205,42510,692,00002,506,752
17Mar 21, 2026 01:0518,46629,295,5999,639,0001,071,0002,289,664
18Mar 20, 2026 02:2818,47929,915,62610,718,00002,510,848
19Mar 21, 2026 02:5818,48326,090,35210,720,00002,289,664
20Mar 22, 2026 22:4118,49527,626,28510,727,00002,514,944
21Mar 15, 2026 21:3118,53627,988,8069,676,0001,075,0002,285,568
22Mar 20, 2026 02:2818,54327,975,85710,755,00002,285,568
23Mar 19, 2026 10:5118,57228,199,6429,695,0001,077,0002,514,944
24Mar 19, 2026 10:5118,60924,580,3029,714,0001,079,0002,285,568
25Mar 20, 2026 02:2818,65032,174,6959,736,0001,081,0002,289,664
26Mar 20, 2026 07:4918,66928,083,7179,746,0001,082,0002,289,664
27Mar 21, 2026 02:5118,72824,398,1678,887,0001,975,0002,510,848
28Mar 21, 2026 02:0318,79129,260,2919,909,000990,0002,289,664
29Mar 20, 2026 00:3618,79526,773,3998,919,0001,982,0002,289,664
30Mar 20, 2026 00:3618,80931,213,5689,918,000991,0002,289,664
31Mar 21, 2026 02:0318,80926,751,5279,918,000991,0002,510,848
32Mar 22, 2026 22:4118,80928,319,2428,926,0001,983,0002,293,760
33Mar 15, 2026 21:3118,81627,851,4229,921,000992,0002,285,568
34Mar 19, 2026 10:5118,82127,357,5439,924,000992,0002,502,656
35Mar 22, 2026 22:4118,82632,108,1869,927,000992,0002,289,664
36Mar 20, 2026 00:3618,84832,466,8118,945,0001,987,0002,510,848
37Mar 19, 2026 10:4818,86625,019,9319,948,000994,0002,506,752
38Mar 19, 2026 10:4818,92827,557,2459,980,000998,0002,506,752
39Mar 19, 2026 10:4819,00928,454,3699,021,0002,004,0002,506,752