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 10, 2026 17:46Tim ZhamoidinTim ZhamoidinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:07012,150,1551,866,000020,480No answer to stdout. Died or killed?
2Apr 10, 2026 17:4174,60773,166,76441,260,0002,012,0002,502,656
3Apr 10, 2026 17:4174,67968,202,29742,307,0001,007,0002,490,368
4Apr 10, 2026 17:4174,87962,755,89142,420,0001,010,0002,285,568
5Apr 10, 2026 17:4174,90365,954,64242,434,0001,010,0002,502,656
6Apr 10, 2026 17:4675,00963,882,29542,494,0001,011,0002,506,752
7Apr 10, 2026 17:4675,02869,957,10342,504,0001,012,0002,285,568
8Apr 11, 2026 16:3875,08468,586,82442,537,0001,012,0002,293,760
9Apr 11, 2026 16:3775,11468,013,86342,553,0001,013,0002,506,752
10Apr 10, 2026 17:4175,17664,082,76742,588,0001,014,0002,494,464
11Apr 11, 2026 16:3775,18369,604,40442,592,0001,014,0002,281,472
12Apr 11, 2026 16:3875,22872,081,74042,618,0001,014,0002,506,752
13Apr 11, 2026 16:3775,26271,329,12542,637,0001,015,0002,498,560
14Apr 10, 2026 17:4675,32869,828,06942,674,0001,016,0002,281,472
15Apr 11, 2026 16:3875,44870,215,58442,743,0001,017,0002,281,472
16Apr 10, 2026 17:4675,46268,159,21643,768,00002,502,656
17Apr 10, 2026 17:4175,63170,294,86042,870,000996,0002,355,200
18Apr 10, 2026 17:4675,63676,202,49741,875,0001,994,0002,285,568
19Apr 10, 2026 17:4175,66970,997,25641,894,0001,994,0002,498,560
20Apr 10, 2026 17:4675,69878,524,53741,910,0001,995,0002,285,568
21Apr 10, 2026 17:4675,80069,121,55541,966,0001,998,0002,519,040
22Apr 10, 2026 17:4675,83171,843,78041,983,0001,999,0002,281,472
23Apr 10, 2026 17:4175,87267,925,66643,006,0001,000,0002,285,568
24Apr 10, 2026 17:4675,87970,231,47042,010,0002,000,0002,281,472
25Apr 10, 2026 17:4176,01466,221,00443,086,0001,002,0002,510,848