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 9, 2026 22:00Code HintsCode HintsScore: 92,905Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 9, 2026 22:0092,32485,568,73251,528,0002,020,0002,351,104
2Feb 9, 2026 22:0092,33684,856,08449,514,0004,041,0002,433,024
3Feb 9, 2026 22:0392,51482,166,08250,621,0003,037,0002,322,432
4Feb 9, 2026 22:0392,65583,465,61050,699,0003,041,0002,379,776
5Feb 9, 2026 22:0092,85279,328,97647,871,0005,983,0002,334,720
6Feb 9, 2026 22:0092,89781,089,24847,894,0005,986,0002,359,296
7Feb 9, 2026 22:0092,90577,871,10847,898,0005,987,0002,301,952
8Feb 9, 2026 22:0092,91677,164,03449,900,0003,991,0002,449,408
9Feb 9, 2026 22:0092,98377,880,65449,936,0003,994,0002,301,952
10Feb 9, 2026 22:0092,98678,883,53150,936,0002,996,0002,367,488
11Feb 9, 2026 22:0393,18385,493,79549,042,0005,004,0002,318,336
12Feb 9, 2026 22:0093,24075,868,13351,075,0003,004,0002,387,968