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 6, 2026 23:43Josu San MartinJosu San MartinScore: 5,824Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 18:135,51918,149,0061,067,0002,134,0002,322,432
2Feb 6, 2026 23:435,59821,728,0892,165,0001,082,0002,314,240
3Dec 28, 2025 18:135,66423,163,8832,190,0001,095,0002,265,088
4Feb 6, 2026 23:435,68120,621,7722,197,0001,098,0002,277,376
5Dec 28, 2025 18:135,73119,302,4682,216,0001,108,0002,490,368
6Dec 28, 2025 18:135,73320,256,1342,217,0001,108,0002,273,280
7Dec 28, 2025 18:135,77120,812,8883,347,00002,412,544
8Dec 28, 2025 18:135,82423,753,9503,378,00002,379,776
9Feb 6, 2026 23:435,85023,344,1682,262,0001,131,0002,273,280
10Dec 28, 2025 18:135,85220,602,1473,394,00002,478,080
11Feb 6, 2026 23:435,85220,667,0082,263,0001,131,0002,347,008
12Dec 28, 2025 18:135,92626,375,8873,437,00002,269,184
13Feb 6, 2026 23:435,93824,100,7982,296,0001,148,0002,277,376
14Feb 6, 2026 23:435,94018,914,9882,297,0001,148,0002,318,336
15Dec 28, 2025 18:135,95024,293,1623,451,00002,457,600