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 listMay 1, 2026 14:35Victor MerckléVictor MerckléScore: 391,505Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1May 1, 2026 14:36387,815247,462,925221,934,0002,999,0002,326,528stderr
2May 1, 2026 14:36390,150248,588,045224,285,0002,002,0002,494,464stderr
3May 1, 2026 14:36390,350241,947,187225,402,0001,001,0002,490,368stderr
4May 1, 2026 14:36390,470241,550,838223,467,0003,006,0002,306,048stderr
5May 1, 2026 14:36391,505246,938,037226,069,0001,004,0002,498,560stderr
6May 1, 2026 14:36392,405249,909,298225,590,0002,005,0002,502,656stderr
7May 1, 2026 14:36392,865245,701,286225,855,0002,007,0002,322,432stderr
8May 1, 2026 14:36394,741244,827,083225,951,0002,999,0002,318,336stderr
9May 1, 2026 14:36397,013248,562,130227,265,0003,003,0002,498,560stderr