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 19, 2025 19:59idatsyidatsyScore: 81,936Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 19:5979,98468,021,99742,357,0004,034,0002,355,200
2Dec 19, 2025 19:5980,02865,284,84842,380,0004,036,0002,404,352
3Dec 19, 2025 19:5981,09570,802,36942,032,0005,003,0002,486,272
4Dec 19, 2025 19:5981,40069,522,42641,185,0006,027,000417,792
5Dec 19, 2025 19:5981,43469,743,38441,203,0006,029,000352,256
6Dec 19, 2025 19:5981,45772,573,67341,340,0005,905,000282,624
7Dec 19, 2025 19:5981,46479,378,16242,223,0005,026,0002,396,160
8Dec 19, 2025 20:0881,69065,744,13942,340,0005,040,0002,486,272
9Dec 19, 2025 20:0881,88171,242,53543,450,0004,041,0002,297,856
10Dec 19, 2025 19:5981,88869,373,35941,432,0006,063,0002,510,848
11Dec 19, 2025 19:5981,93672,302,73741,457,0006,066,0002,486,272
12Dec 19, 2025 19:5982,14072,306,00842,573,0005,068,0002,433,024
13Dec 19, 2025 19:5982,43372,702,11643,742,0004,069,000212,992
14Dec 19, 2025 20:0882,59576,502,20541,917,0005,988,0002,510,848
15Dec 19, 2025 19:5983,24376,165,86141,240,0007,041,0002,473,984
16Dec 19, 2025 19:5983,96972,759,12042,739,0005,963,0002,441,216
17Dec 19, 2025 19:5984,47171,635,97441,994,0006,999,000356,352
18Dec 19, 2025 19:5984,65273,431,33143,086,0006,012,0002,478,080
19Dec 19, 2025 19:5987,30396,478,33742,535,0008,101,0002,281,472
20Dec 19, 2025 19:5991,45388,343,96845,903,0007,140,0002,285,568
21Dec 19, 2025 19:5993,202127,418,65743,046,00011,011,0002,457,600