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 10, 2026 19:15undenuicapundenuicapError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 13:27Andrew Epstein23.04CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 13:27010,285,286770,000020,480No answer to stdout. Died or killed?
2Feb 10, 2026 19:1240,34546,056,99222,383,0001,017,0002,506,752
3Feb 10, 2026 19:1240,41649,723,50222,422,0001,019,0002,506,752
4Feb 10, 2026 19:1541,21960,767,57822,911,000996,0002,486,272
5Feb 10, 2026 19:1641,96444,250,69424,339,00002,461,696
6Feb 10, 2026 19:1242,04746,452,06424,387,00002,473,984
7Feb 10, 2026 19:1642,53448,119,49724,670,00002,494,464
8Feb 10, 2026 19:1642,95947,738,39023,920,000996,0002,482,176
9Feb 10, 2026 19:1643,00350,449,16322,947,0001,995,0002,498,560
10Feb 10, 2026 19:1643,03351,795,74423,961,000998,0002,478,080
11Feb 10, 2026 19:1243,13453,273,95124,018,0001,000,0002,478,080
12Feb 10, 2026 19:1243,41054,815,02223,164,0002,014,0002,473,984
13Feb 10, 2026 19:1243,71052,098,71925,352,00002,498,560
14Feb 10, 2026 19:1644,00748,532,02225,524,00002,490,368
15Feb 10, 2026 19:1244,21650,093,63025,645,00002,342,912
16Feb 10, 2026 19:1244,99148,877,60424,088,0002,007,0002,469,888
17Feb 10, 2026 19:1645,03646,376,80726,121,00002,285,568
18Feb 10, 2026 19:1245,18652,630,17723,184,0003,024,0002,457,600
19Feb 10, 2026 19:1645,29349,095,12123,239,0003,031,0002,498,560
20Feb 10, 2026 19:1645,30948,028,19823,247,0003,032,0002,486,272
21Feb 10, 2026 19:1546,00354,495,00526,682,00002,482,176
22Feb 10, 2026 19:1546,65050,352,92125,053,0002,004,0002,473,984