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 12:15Dominique 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:10023,422,2968,160,0004,299,00016,384No answer to stdout. Died or killed?
2Mar 19, 2026 12:5617,49726,901,0238,119,0002,029,0002,281,472
3Mar 19, 2026 12:5617,69526,164,2338,211,0002,052,0002,289,664
4Mar 19, 2026 13:1917,71227,044,9959,246,0001,027,0002,277,376
5Mar 19, 2026 12:3717,72930,801,82810,283,00002,269,184
6Mar 19, 2026 13:1917,74528,774,54610,292,00002,277,376
7Mar 19, 2026 12:5117,75926,867,8899,270,0001,030,0002,285,568
8Mar 19, 2026 12:5117,76428,325,85910,303,00002,289,664
9Mar 19, 2026 12:1517,76727,442,37310,305,00002,273,280
10Mar 19, 2026 13:3017,76927,226,26910,306,00002,281,472
11Mar 19, 2026 12:1317,77827,764,1629,280,0001,031,0002,531,328
12Mar 19, 2026 12:1317,77826,333,5769,280,0001,031,0002,285,568
13Mar 19, 2026 12:5917,78328,738,2019,283,0001,031,0002,289,664
14Mar 19, 2026 12:5917,79727,129,50510,322,00002,285,568
15Mar 19, 2026 12:1317,81425,085,7279,299,0001,033,0002,273,280
16Mar 19, 2026 12:4417,83125,825,6029,308,0001,034,0002,289,664
17Mar 19, 2026 12:5617,83430,871,31710,344,00002,273,280
18Mar 19, 2026 13:3017,83628,115,1979,311,0001,034,0002,289,664
19Mar 19, 2026 12:4417,88827,442,44410,375,00002,273,280
20Mar 19, 2026 13:0417,90030,158,1549,344,0001,038,0002,277,376
21Mar 19, 2026 13:3017,90526,340,9599,347,0001,038,0002,289,664
22Mar 19, 2026 12:1717,91931,881,39310,393,00002,273,280
23Mar 19, 2026 13:1917,92131,121,8379,355,0001,039,0002,277,376
24Mar 19, 2026 12:1717,93429,092,6359,362,0001,040,0002,277,376
25Mar 19, 2026 12:3718,01027,851,2879,402,0001,044,0002,387,968
26Mar 19, 2026 13:0418,03129,146,4599,413,0001,045,0002,273,280
27Mar 19, 2026 12:5918,06228,180,2909,429,0001,047,0002,281,472
28Mar 19, 2026 12:1718,06233,133,3399,429,0001,047,0002,269,184
29Mar 19, 2026 12:3718,08629,883,6049,441,0001,049,0002,281,472
30Mar 19, 2026 13:0418,11730,865,1329,458,0001,050,0002,289,664
31Mar 19, 2026 12:1518,18324,640,8589,492,0001,054,0002,289,664
32Mar 19, 2026 12:5118,23828,291,2009,521,0001,057,0002,523,136
33Mar 19, 2026 12:4418,28830,224,8099,547,0001,060,0002,273,280
34Mar 19, 2026 12:1518,48333,609,8299,648,0001,072,0002,277,376