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 listSep 29, 2025 20:20Wookyung JeongWookyung JeongScore: 55,562Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 20:2052,35359,887,47628,341,0002,024,0002,453,504
2Sep 29, 2025 20:2152,51752,642,17930,460,00002,301,952
3Sep 29, 2025 20:1952,69754,186,72729,546,0001,018,0002,363,392
4Sep 29, 2025 20:1952,99157,328,00129,711,0001,024,0002,322,432
5Sep 29, 2025 20:2053,00556,091,19828,694,0002,049,0002,392,064
6Sep 29, 2025 20:2153,58153,922,36826,065,0005,012,0002,437,120
7Sep 29, 2025 20:2153,61456,450,47728,087,0003,009,0002,310,144
8Sep 29, 2025 20:2154,01952,875,94228,299,0003,032,0002,519,040
9Sep 29, 2025 20:2054,44158,285,20228,521,0003,055,0002,379,776
10Sep 29, 2025 20:2154,55558,521,80329,601,0002,041,0002,420,736
11Sep 29, 2025 20:2154,96960,992,43930,886,000996,0002,506,752
12Sep 29, 2025 20:2054,98159,503,72129,896,0001,993,0002,465,792
13Sep 29, 2025 20:2155,01659,809,95728,918,0002,991,0002,441,216
14Sep 29, 2025 20:2155,29158,263,27731,067,0001,002,0002,396,160
15Sep 29, 2025 20:2155,38851,705,18530,118,0002,007,0002,301,952
16Sep 29, 2025 20:1955,56255,050,74428,198,0004,028,0002,310,144
17Sep 29, 2025 20:2055,60255,247,75028,218,0004,031,0002,375,680
18Sep 29, 2025 20:2155,60357,117,90029,227,0003,023,0002,297,856
19Sep 29, 2025 20:2155,76759,031,59629,313,0003,032,0002,273,280
20Sep 29, 2025 20:2155,81255,696,44028,325,0004,046,0002,326,528
21Sep 29, 2025 20:2155,89056,673,95429,377,0003,039,0002,371,584
22Sep 29, 2025 20:2156,04154,649,25630,473,0002,031,0002,359,296
23Sep 29, 2025 20:2056,39559,679,92431,687,0001,022,0002,424,832
24Sep 29, 2025 20:2156,91054,697,14629,007,0004,001,0002,383,872
25Sep 29, 2025 20:2056,99354,891,68931,053,0002,003,0002,441,216
26Sep 29, 2025 20:2158,16459,481,74931,691,0002,044,0002,420,736
27Sep 29, 2025 20:2158,44058,902,55429,908,0003,987,0002,457,600
28Sep 29, 2025 20:2158,47459,289,69530,832,0003,083,0002,428,928
29Sep 29, 2025 20:2058,61659,152,78030,998,0002,999,0002,371,584
30Sep 29, 2025 20:2058,78459,212,98731,087,0003,008,0002,347,008