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 listApr 13, 2025 18:11yenw0dyenw0dError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 13, 2025 18:1100000Error: expected "2960405 ", got "0 "
2Apr 13, 2025 18:1165,89365,202,20337,213,0001,005,0002,277,376
3Apr 13, 2025 18:1167,75962,037,02338,293,0001,007,0002,510,848
4Apr 13, 2025 18:1168,00055,391,61138,429,0001,011,0002,330,624
5Apr 13, 2025 18:1168,06666,592,54038,466,0001,012,0002,453,504
6Apr 13, 2025 18:1168,10263,917,67138,487,0001,012,0002,461,696
7Apr 13, 2025 18:1168,76266,897,77837,888,0001,994,0002,494,464
8Apr 13, 2025 18:1168,88373,301,29737,955,0001,997,0002,449,408
9Apr 13, 2025 18:1169,74767,019,96140,453,00002,469,888
10Apr 13, 2025 18:1169,96662,085,85439,566,0001,014,0002,506,752