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 listDec 28, 2025 17:57imotw2imotw2Score: 92,914Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 17:5390,00975,271,03948,190,0004,015,0002,465,792
2Dec 28, 2025 17:5790,23375,861,45050,323,0002,012,0002,301,952
3Dec 28, 2025 17:5390,52877,231,95449,477,0003,029,0002,482,176
4Dec 28, 2025 18:0791,18684,324,33949,895,0002,993,0002,486,272
5Dec 28, 2025 18:0791,31475,480,04548,965,0003,997,0002,478,080
6Dec 28, 2025 18:0791,48374,916,66349,056,0004,004,0002,347,008
7Dec 28, 2025 18:0791,66779,051,13650,158,0003,009,0002,486,272
8Dec 28, 2025 18:0791,69175,135,62150,171,0003,010,0002,416,640
9Dec 28, 2025 17:5391,93376,032,36349,297,0004,024,0002,326,528
10Dec 28, 2025 17:5392,06274,368,60948,359,0005,037,0002,301,952
11Dec 28, 2025 18:0792,27481,605,63350,490,0003,029,0002,457,600
12Dec 28, 2025 17:5392,30976,966,67749,499,0004,040,0002,310,144
13Dec 28, 2025 18:0692,91481,083,98247,903,0005,987,0002,326,528
14Dec 28, 2025 18:0693,16677,912,58948,032,0006,004,0002,478,080
15Dec 28, 2025 18:0793,29779,032,65649,102,0005,010,0002,347,008
16Dec 28, 2025 17:5393,69583,298,88350,318,0004,025,0002,318,336
17Dec 28, 2025 17:5394,19881,842,81052,612,0002,023,0002,297,856
18Dec 28, 2025 17:5394,62181,007,51551,887,0002,993,0002,461,696
19Dec 28, 2025 17:5794,66485,111,42550,912,0003,993,0002,457,600
20Dec 28, 2025 18:0794,67985,238,32849,922,0004,992,0002,314,240
21Dec 28, 2025 17:5395,07278,483,05751,132,0004,010,0002,445,312
22Dec 28, 2025 17:5795,09876,619,26052,149,0003,008,0002,281,472
23Dec 28, 2025 18:0795,11980,116,40451,157,0004,012,0002,437,120
24Dec 28, 2025 18:0695,14078,512,55549,162,0006,019,0002,301,952