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 20, 2026 16:30BenBenScore: 139,563Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 20, 2026 16:32138,717103,696,16477,439,0003,017,0002,494,464
2Apr 20, 2026 16:32138,922102,449,31677,554,0003,021,0002,494,464
3Apr 20, 2026 16:32138,984111,390,14277,589,0003,022,0002,502,656
4Apr 20, 2026 16:32139,075118,547,72276,631,0004,033,0002,277,376
5Apr 20, 2026 16:32139,563106,568,76277,949,0002,998,0002,494,464
6Apr 20, 2026 16:32139,801108,286,85077,081,0004,004,0002,506,752
7Apr 20, 2026 16:32139,805101,502,45978,084,0003,003,0002,293,760
8Apr 20, 2026 16:32139,924103,590,16578,151,0003,005,0002,285,568
9Apr 20, 2026 16:32140,055103,570,56477,221,0004,011,0002,490,368