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 21, 2026 09:32Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:06Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:06012,240,6172,270,000020,480No answer to stdout. Died or killed?
2Apr 21, 2026 09:3210,44424,762,6624,039,0002,019,0002,281,472
3Apr 21, 2026 09:3211,50626,266,2665,562,0001,112,0002,273,280
4Apr 21, 2026 09:3211,52525,325,8945,571,0001,114,0002,269,184
5Apr 21, 2026 09:3211,53424,296,9075,575,0001,115,0002,289,664
6Apr 21, 2026 09:3211,60620,467,2315,610,0001,122,0002,277,376
7Apr 21, 2026 09:3211,84326,442,0785,888,000981,0002,494,464
8Apr 21, 2026 09:3211,91525,552,6715,924,000987,0002,277,376
9Apr 21, 2026 09:3211,99630,635,3784,970,0001,988,0002,289,664
10Apr 21, 2026 09:3212,75123,031,5126,340,0001,056,0002,289,664