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 listAug 14, 2026 22:58marko1597marko1597Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 13:27Andrew Epstein15.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 13:28010,652,656868,000020,480No answer to stdout. Died or killed?
2Aug 14, 2026 22:5850,00538,744,47429,003,00002,113,536
3Aug 14, 2026 22:5850,22438,945,99629,130,00002,113,536
4Aug 14, 2026 22:5850,39139,035,83429,227,00002,113,536
5Aug 14, 2026 22:5850,58739,194,26129,341,00002,113,536
6Aug 14, 2026 22:5850,85040,027,65229,493,00002,113,536
7Aug 14, 2026 22:5850,96839,358,30429,562,00002,113,536
8Aug 14, 2026 22:5851,15139,499,35329,668,00002,113,536
9Aug 14, 2026 22:5851,32939,544,44428,695,0001,076,0002,113,536
10Aug 14, 2026 22:5851,57439,744,99329,913,00002,113,536