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:35DmtryDmtryError
Source Code

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

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