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 GarmierError
Source Code

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

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