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 21, 2025 04:06Josu San MartinJosu San MartinScore: 37,176Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 04:0635,46747,048,91319,543,0001,028,0002,453,504
2Dec 21, 2025 04:0536,05748,519,97018,922,0001,991,0002,473,984
3Dec 21, 2025 04:0536,08346,005,32118,935,0001,993,0002,465,792
4Dec 21, 2025 04:0636,26447,758,04719,030,0002,003,0002,437,120
5Dec 21, 2025 04:2436,28443,658,78420,043,0001,002,0002,269,184
6Dec 21, 2025 04:0636,31946,544,73518,056,0003,009,0002,469,888
7Dec 21, 2025 04:1036,67145,799,21620,257,0001,012,0002,379,776
8Dec 21, 2025 04:0636,68443,687,25920,264,0001,013,0002,383,872
9Dec 21, 2025 04:1136,74544,765,92421,312,00002,416,640
10Dec 21, 2025 04:2536,82643,341,84720,342,0001,017,0002,301,952
11Dec 21, 2025 04:1036,92144,547,84720,395,0001,019,0002,301,952
12Dec 21, 2025 04:0536,95246,159,01920,412,0001,020,0002,519,040
13Dec 21, 2025 04:0637,08441,322,33720,485,0001,024,0002,428,928
14Dec 21, 2025 04:0637,11644,163,32619,477,0002,050,0002,347,008
15Dec 21, 2025 04:2437,13147,380,07520,511,0001,025,0002,367,488
16Dec 21, 2025 04:2437,17643,516,22321,562,00002,375,680
17Dec 21, 2025 04:2537,22943,873,10220,565,0001,028,0002,347,008
18Dec 21, 2025 04:0637,29343,888,17920,600,0001,030,0002,412,544
19Dec 21, 2025 04:1137,30051,707,67521,634,00002,371,584
20Dec 21, 2025 04:2437,30548,500,20521,637,00002,347,008
21Dec 21, 2025 04:0637,35051,295,00520,632,0001,031,0002,371,584
22Dec 21, 2025 04:0637,36744,606,98220,641,0001,032,0002,400,256
23Dec 21, 2025 04:0637,37251,198,83520,644,0001,032,0002,375,680
24Dec 21, 2025 04:2437,49550,080,23621,747,00002,379,776
25Dec 21, 2025 04:2537,51247,730,28420,721,0001,036,0002,449,408
26Dec 21, 2025 04:1037,76450,712,39619,912,0001,991,0002,416,640
27Dec 21, 2025 04:2437,77446,659,69819,918,0001,991,0002,404,352
28Dec 21, 2025 04:1137,77948,392,40518,924,0002,988,0002,306,048
29Dec 21, 2025 04:0637,86951,011,48120,966,000998,0002,359,296
30Dec 21, 2025 04:0638,08652,719,78520,082,0002,008,0002,465,792