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 listFeb 19, 2026 02:16Josu San MartinJosu San MartinScore: 3,755Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 19, 2026 02:243,54316,852,75602,055,0002,310,144
2Feb 19, 2026 02:213,64121,489,7581,056,0001,056,0002,330,624
3Feb 19, 2026 02:223,67920,224,3801,067,0001,067,0002,322,432
4Feb 19, 2026 02:243,69320,392,3501,071,0001,071,0002,326,528
5Feb 19, 2026 02:213,70316,975,2451,074,0001,074,0002,318,336
6Feb 19, 2026 02:253,70319,417,6161,074,0001,074,0002,289,664
7Feb 19, 2026 02:253,73317,585,12002,165,0002,285,568
8Feb 19, 2026 02:253,75521,208,5461,089,0001,089,0002,269,184
9Feb 19, 2026 02:243,75718,444,15202,179,0002,285,568
10Feb 19, 2026 02:163,77919,889,7831,096,0001,096,0002,465,792
11Feb 19, 2026 02:213,79719,720,5481,101,0001,101,0002,498,560
12Feb 19, 2026 02:223,79720,711,7901,101,0001,101,0002,494,464
13Feb 19, 2026 02:223,82119,021,0941,108,0001,108,0002,396,160
14Feb 19, 2026 02:163,86018,515,3012,239,00002,441,216
15Feb 19, 2026 02:163,90727,491,6071,133,0001,133,0002,441,216