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 24, 2026 14:35DmtryDmtryError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 14:57Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 14:5809,503,6581,649,00002,113,536expected "999999000 ", got "0 "
2Jul 5, 2026 14:58010,372,3351,671,00002,113,536expected "999999000 ", got "0 "
3Jul 5, 2026 14:5809,276,3871,648,00002,113,536expected "999999000 ", got "0 "
4Mar 23, 2026 17:294,16718,146,7912,417,00002,465,792
5Mar 23, 2026 18:294,16723,096,7072,417,00002,457,600
6Mar 23, 2026 17:434,22621,092,3222,451,00002,457,600
7Mar 23, 2026 17:294,25016,458,4122,465,00002,461,696
8Mar 23, 2026 22:214,25919,690,4122,470,00002,457,600
9Mar 24, 2026 14:354,26722,586,5812,475,00002,461,696
10Mar 23, 2026 17:374,27122,001,5162,477,00002,449,408
11Mar 23, 2026 17:294,27417,664,9812,479,00002,465,792
12Mar 23, 2026 18:294,27420,661,9622,479,00002,469,888
13Mar 23, 2026 22:114,27617,653,8212,480,00002,457,600
14Mar 23, 2026 17:374,27817,606,7352,481,00002,453,504
15Mar 23, 2026 22:114,28318,273,2472,484,00002,473,984
16Mar 23, 2026 17:434,29516,097,7952,491,00002,457,600
17Mar 23, 2026 17:324,29518,868,6562,491,00002,457,600
18Mar 23, 2026 17:324,30018,244,5562,494,00002,465,792
19Mar 23, 2026 22:104,32418,739,2231,254,0001,254,0002,469,888
20Mar 23, 2026 18:294,32819,677,5792,510,00002,457,600
21Mar 23, 2026 17:434,33318,961,3832,513,00002,461,696
22Mar 23, 2026 21:554,33418,646,7902,514,00002,457,600
23Mar 23, 2026 17:374,36221,300,8542,530,00002,461,696
24Mar 23, 2026 17:324,40024,205,7252,552,00002,461,696
25Mar 23, 2026 21:554,42818,188,7842,568,00002,535,424
26Mar 23, 2026 22:104,48617,571,3361,301,0001,301,0002,457,600
27Mar 23, 2026 22:214,54525,089,4172,636,00002,457,600
28Mar 24, 2026 14:354,55516,592,0081,321,0001,321,0002,527,232
29Mar 23, 2026 21:554,60026,339,2401,334,0001,334,0002,449,408
30Mar 24, 2026 14:354,61717,861,5161,339,0001,339,0002,465,792
31Mar 23, 2026 22:214,64126,588,1771,346,0001,346,0002,461,696
32Mar 23, 2026 22:104,65515,876,1971,350,0001,350,0002,461,696
33Mar 23, 2026 22:114,74116,035,8631,375,0001,375,0002,449,408