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:58Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:07Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:07030,917,80521,576,000016,384No answer to stdout. Died or killed?
2Dec 18, 2025 16:5832,00542,475,34218,563,00002,269,184
3Dec 18, 2025 16:5832,33344,570,00518,753,00002,506,752
4Dec 18, 2025 16:5832,58845,818,70617,907,000994,0002,269,184
5Dec 18, 2025 16:5832,78638,707,79618,016,0001,000,0002,506,752
6Dec 18, 2025 16:5832,80340,247,56018,025,0001,001,0002,506,752
7Dec 18, 2025 16:5832,88640,509,05217,067,0002,007,0002,482,176
8Dec 18, 2025 16:5833,15341,381,13518,217,0001,012,0002,363,392
9Dec 18, 2025 16:5833,36444,611,45118,333,0001,018,0002,482,176
10Dec 18, 2025 16:5833,37446,648,82118,339,0001,018,0002,342,912
11Dec 18, 2025 16:5833,40045,167,99919,372,00002,486,272
12Dec 18, 2025 16:5833,41642,016,46019,381,00002,490,368
13Dec 18, 2025 16:5833,47246,437,90119,414,00002,265,088
14Dec 18, 2025 16:5833,59042,733,00018,457,0001,025,0002,461,696
15Dec 18, 2025 16:5833,64741,466,89319,515,00002,400,256
16Dec 18, 2025 16:5833,69741,293,23018,516,0001,028,0002,387,968
17Dec 18, 2025 16:5833,72243,442,63518,530,0001,029,0002,293,760
18Dec 18, 2025 16:5833,72641,927,36019,561,00002,408,448
19Dec 18, 2025 16:5834,33343,811,78817,922,0001,991,0002,506,752