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:35DmtryDmtryScore: 4,324Success
Source Code

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

Challenge History

No challenges yet.

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