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 listJan 1, 2026 17:30yenw0dyenw0dError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:11Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:11013,894,19804,517,0002,121,728expected "999999000 ", got "0 "
2Jul 5, 2026 20:11014,205,51504,694,0002,121,728expected "999999000 ", got "0 "
3Jul 5, 2026 20:11013,937,58904,507,0002,121,728expected "999999000 ", got "0 "
4Jan 1, 2026 17:3311,91924,279,4511,975,0004,938,0002,285,568
5Jan 1, 2026 17:3111,99324,004,3641,987,0004,969,0002,465,792
6Jan 1, 2026 17:3012,06225,396,6621,999,0004,997,0002,285,568
7Jan 1, 2026 17:3012,09325,896,7652,004,0005,010,0002,416,640
8Jan 1, 2026 17:3212,09522,417,6952,004,0005,011,0002,379,776
9Jan 1, 2026 17:2912,16021,430,9982,015,0005,038,0002,383,872
10Jan 1, 2026 17:3212,21724,049,7393,037,0004,049,0002,494,464
11Jan 1, 2026 17:3312,39726,871,2693,081,0004,109,0002,289,664
12Jan 1, 2026 17:3012,45026,702,7612,063,0005,158,0002,428,928
13Jan 1, 2026 17:3112,48827,873,8673,104,0004,139,0002,523,136
14Jan 1, 2026 17:2912,59328,371,0562,087,0005,217,0002,297,856
15Jan 1, 2026 17:3212,62129,490,6373,137,0004,183,0002,473,984
16Jan 1, 2026 17:3212,63124,018,2142,093,0005,233,0002,523,136
17Jan 1, 2026 17:3212,69826,883,4881,052,0006,313,0002,342,912
18Jan 1, 2026 17:3312,78128,104,0251,059,0006,354,0002,306,048
19Jan 1, 2026 17:2912,85322,465,1963,195,0004,260,0002,363,392
20Jan 1, 2026 17:3013,04324,739,7962,161,0005,404,0002,473,984
21Jan 1, 2026 17:3113,04322,299,5231,080,0006,485,0002,514,944
22Jan 1, 2026 17:3013,25227,504,0431,098,0006,588,0002,498,560
23Jan 1, 2026 17:3213,69023,344,8593,970,0003,970,0002,269,184
24Jan 1, 2026 17:3013,73323,581,5251,991,0005,974,0002,514,944