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 listDec 18, 2025 16:45Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:42Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:42015,463,0306,034,000020,480No answer to stdout. Died or killed?
2Dec 18, 2025 16:4435,72946,463,58619,687,0001,036,0002,269,184
3Dec 18, 2025 16:4436,05338,278,12120,911,00002,297,856
4Dec 18, 2025 16:4536,16750,843,88718,980,0001,997,0002,260,992
5Dec 18, 2025 16:4436,26944,400,30720,035,0001,001,0002,293,760
6Dec 18, 2025 16:4536,27444,087,94719,036,0002,003,0002,519,040
7Dec 18, 2025 16:4436,46244,011,28619,134,0002,014,0002,494,464
8Dec 18, 2025 16:4536,46643,126,24419,136,0002,014,0002,502,656
9Dec 18, 2025 16:4436,86048,903,80520,361,0001,018,0002,478,080
10Dec 18, 2025 16:4537,05046,160,14421,489,00002,506,752
11Dec 18, 2025 16:4537,14143,280,23120,517,0001,025,0002,277,376
12Dec 18, 2025 16:4437,15343,387,78421,549,00002,502,656
13Dec 18, 2025 16:4537,20341,452,08221,578,00002,510,848
14Dec 18, 2025 16:4437,21445,180,05920,557,0001,027,0002,502,656
15Dec 18, 2025 16:4437,26045,085,35220,582,0001,029,0002,482,176
16Dec 18, 2025 16:4437,31444,105,44021,642,00002,260,992
17Dec 18, 2025 16:4437,32246,671,42021,647,00002,441,216
18Dec 18, 2025 16:4537,36247,304,25721,670,00002,482,176
19Dec 18, 2025 16:4537,41447,693,87620,667,0001,033,0002,322,432
20Dec 18, 2025 16:4537,46652,260,04620,696,0001,034,0002,461,696
21Dec 18, 2025 16:4537,47848,037,14920,702,0001,035,0002,494,464
22Dec 18, 2025 16:4437,49548,195,83820,712,0001,035,0002,260,992
23Dec 18, 2025 16:4537,50248,154,85820,716,0001,035,0002,428,928
24Dec 18, 2025 16:4537,74748,820,50920,898,000995,0002,433,024
25Dec 18, 2025 16:4438,01644,991,17620,045,0002,004,0002,375,680