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 28, 2025 22:25Yuriy LyfenkoYuriy LyfenkoScore: 46,802Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 28, 2025 22:2542,32249,003,11723,525,0001,022,0002,519,040
2Apr 28, 2025 22:2442,92454,278,57723,901,000995,0002,338,816
3Apr 28, 2025 22:2442,97950,171,47023,890,0001,038,0002,490,368
4Apr 28, 2025 22:2445,33446,264,13625,283,0001,011,0002,297,856
5Apr 28, 2025 22:2446,10951,300,18326,743,00002,293,760
6Apr 28, 2025 22:2446,38159,420,63124,909,0001,992,0002,310,144
7Apr 28, 2025 22:2446,52946,274,16025,988,000999,0002,301,952
8Apr 28, 2025 22:2446,80248,083,14026,140,0001,005,0002,478,080
9Apr 28, 2025 22:2447,22454,247,16926,376,0001,014,0002,301,952
10Apr 28, 2025 22:2548,13351,755,88527,917,00002,519,040
11Apr 28, 2025 22:2450,03154,019,85628,018,0001,000,0002,510,848
12Apr 28, 2025 22:2550,06450,395,27928,036,0001,001,0002,498,560
13Apr 28, 2025 22:2550,23850,237,51728,134,0001,004,0002,445,312
14Apr 28, 2025 22:2550,66250,042,74529,384,00002,285,568
15Apr 28, 2025 22:2555,01449,631,47130,879,0001,029,0002,408,448