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 00:00Josu San MartinJosu San MartinScore: 4,259Success
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 00:004,12420,683,2572,392,00002,277,376
2Feb 18, 2026 23:554,15224,366,1241,204,0001,204,0002,478,080
3Feb 18, 2026 23:584,23421,994,9781,228,0001,228,0002,510,848
4Feb 18, 2026 23:554,25919,171,4192,470,00002,527,232
5Feb 18, 2026 23:584,25921,106,0481,235,0001,235,0002,465,792
6Feb 18, 2026 23:554,26718,134,7652,475,00002,445,312
7Feb 19, 2026 00:004,27418,843,4132,479,00002,310,144
8Feb 19, 2026 00:004,30726,303,6012,498,00002,510,848
9Feb 18, 2026 23:584,34519,110,1501,260,0001,260,0002,498,560