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: 3,850Success
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jun 1, 2026 03:37Josu San Martin21.44RecheckSolution Held
Jun 1, 2026 01:23Josu San Martin10.00RecheckSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jun 1, 2026 03:373,5019,286,6142,031,00002,117,632
2Jun 1, 2026 03:373,7629,564,4622,182,00002,117,632
3Jun 1, 2026 01:243,82914,410,0692,221,00002,117,632
4Jun 1, 2026 03:373,8509,599,1252,233,00002,117,632
5Jun 1, 2026 01:243,92711,989,4162,278,00002,117,632
6Jun 1, 2026 01:244,0679,829,8482,359,00002,117,632
7Mar 23, 2026 13:414,13415,837,2352,398,00002,457,600
8Mar 23, 2026 13:414,14116,750,5392,402,00002,457,600
9Mar 23, 2026 12:354,15016,354,1922,407,00002,465,792
10Mar 23, 2026 12:354,16022,488,4862,413,00002,461,696
11Mar 23, 2026 13:414,23119,312,4912,454,00002,449,408
12Mar 23, 2026 12:354,23317,631,1472,455,00002,453,504
13Mar 23, 2026 13:414,25220,149,6982,466,00002,535,424
14Mar 23, 2026 12:354,25315,991,8912,467,00002,535,424
15Mar 23, 2026 13:414,25918,482,7502,470,00002,457,600
16Mar 23, 2026 12:354,25917,971,6622,470,00002,453,504
17Mar 23, 2026 12:354,26716,786,8902,475,00002,453,504
18Mar 23, 2026 13:414,28614,245,2421,243,0001,243,0002,461,696
19Mar 23, 2026 12:354,30515,411,5922,497,00002,457,600
20Mar 23, 2026 12:354,30918,559,0142,499,00002,457,600
21Mar 23, 2026 13:414,33321,089,6112,513,00002,457,600
22Mar 23, 2026 12:354,42916,432,1022,569,00002,457,600
23Mar 23, 2026 13:414,50219,742,2972,611,00002,453,504
24Mar 23, 2026 13:414,52414,952,5991,312,0001,312,0002,461,696