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 19, 2025 19:49Aniruddha DebAniruddha DebError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:34Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:3409,900,9432,219,00002,121,728expected "999999000 ", got "0 "stderr
2Jul 5, 2026 20:3409,885,2322,215,00002,121,728expected "999999000 ", got "0 "stderr
3Jul 5, 2026 20:3409,873,6762,232,00002,121,728expected "999999000 ", got "0 "stderr
4Apr 19, 2025 19:4913,47421,873,7226,699,0001,116,0002,494,464
5Apr 19, 2025 19:4914,83422,577,6238,604,00002,519,040
6Apr 19, 2025 19:4916,90931,976,8579,807,00002,519,040
7Apr 19, 2025 19:4918,89725,677,0209,964,000996,0002,506,752
8Apr 19, 2025 19:4919,19827,860,7649,111,0002,024,0002,482,176
9Apr 19, 2025 19:4919,30227,450,75510,178,0001,017,0002,498,560
10Apr 19, 2025 19:4919,78325,284,82511,474,00002,510,848
11Apr 19, 2025 19:4922,76630,971,78312,189,0001,015,0002,506,752
12Apr 19, 2025 19:4925,93433,659,88014,040,0001,002,0002,506,752