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 23, 2026 12:35DmtryDmtryScore: 4,259Success
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 13:414,13415,837,2352,398,00002,457,600
2Mar 23, 2026 13:414,14116,750,5392,402,00002,457,600
3Mar 23, 2026 12:354,15016,354,1922,407,00002,465,792
4Mar 23, 2026 12:354,16022,488,4862,413,00002,461,696
5Mar 23, 2026 13:414,23119,312,4912,454,00002,449,408
6Mar 23, 2026 12:354,23317,631,1472,455,00002,453,504
7Mar 23, 2026 13:414,25220,149,6982,466,00002,535,424
8Mar 23, 2026 12:354,25315,991,8912,467,00002,535,424
9Mar 23, 2026 12:354,25917,971,6622,470,00002,453,504
10Mar 23, 2026 13:414,25918,482,7502,470,00002,457,600
11Mar 23, 2026 12:354,26716,786,8902,475,00002,453,504
12Mar 23, 2026 13:414,28614,245,2421,243,0001,243,0002,461,696
13Mar 23, 2026 12:354,30515,411,5922,497,00002,457,600
14Mar 23, 2026 12:354,30918,559,0142,499,00002,457,600
15Mar 23, 2026 13:414,33321,089,6112,513,00002,457,600
16Mar 23, 2026 12:354,42916,432,1022,569,00002,457,600
17Mar 23, 2026 13:414,50219,742,2972,611,00002,453,504
18Mar 23, 2026 13:414,52414,952,5991,312,0001,312,0002,461,696