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:13Wookyung JeongWookyung JeongScore: 55,078Success
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:1351,55354,462,11428,905,000996,0002,519,040
2Sep 29, 2025 20:1451,70752,551,42527,991,0001,999,0002,297,856
3Sep 29, 2025 20:1452,52656,100,51027,419,0003,046,0002,523,136
4Sep 29, 2025 20:1252,70957,084,35326,495,0004,076,0002,486,272
5Sep 29, 2025 20:1352,97956,887,99130,728,00002,285,568
6Sep 29, 2025 20:1253,03456,647,24528,710,0002,050,0002,355,200
7Sep 29, 2025 20:1253,22660,937,28827,884,0002,987,0002,306,048
8Sep 29, 2025 20:1353,25760,265,71527,900,0002,989,0002,363,392
9Sep 29, 2025 20:1553,27160,094,36226,911,0003,986,0002,498,560
10Sep 29, 2025 20:1453,57656,198,91928,067,0003,007,0002,445,312
11Sep 29, 2025 20:1353,65556,634,79828,109,0003,011,0002,392,064
12Sep 29, 2025 20:1453,70955,602,91727,132,0004,019,0002,437,120
13Sep 29, 2025 20:1253,80958,943,43827,182,0004,027,0002,383,872
14Sep 29, 2025 20:1453,95754,811,59029,276,0002,019,0002,379,776
15Sep 29, 2025 20:1254,01651,280,94228,298,0003,031,0002,433,024
16Sep 29, 2025 20:1254,18653,258,04327,373,0004,055,0002,367,488
17Sep 29, 2025 20:1254,20056,336,41727,380,0004,056,0002,318,336
18Sep 29, 2025 20:1454,24558,281,20428,418,0003,044,0002,469,888
19Sep 29, 2025 20:1454,45053,700,40530,563,0001,018,0002,342,912
20Sep 29, 2025 20:1255,07851,464,72428,951,0002,994,0002,416,640
21Sep 29, 2025 20:1555,18658,393,74730,008,0002,000,0002,306,048
22Sep 29, 2025 20:1455,20953,421,67229,019,0003,002,0002,379,776
23Sep 29, 2025 20:1355,25055,515,91828,040,0004,005,0002,498,560
24Sep 29, 2025 20:1255,96455,883,69930,431,0002,028,0002,367,488
25Sep 29, 2025 20:1456,11959,992,60630,515,0002,034,0002,285,568
26Sep 29, 2025 20:1356,28455,563,59231,625,0001,020,0002,301,952
27Sep 29, 2025 20:1356,33457,100,85830,632,0002,042,0002,408,448
28Sep 29, 2025 20:1356,65059,351,75630,866,0001,991,0002,437,120
29Sep 29, 2025 20:1356,67158,265,47230,877,0001,992,0002,322,432
30Sep 29, 2025 20:1456,67260,560,92929,882,0002,988,0002,314,240
31Sep 29, 2025 20:1356,68157,673,86129,887,0002,988,0002,265,088
32Sep 29, 2025 20:1356,68658,460,62430,886,0001,992,0002,371,584
33Sep 29, 2025 20:1456,72263,782,69029,909,0002,990,0002,490,368
34Sep 29, 2025 20:1556,72962,478,05828,915,0003,988,0002,330,624
35Sep 29, 2025 20:1357,04562,188,35630,079,0003,007,0002,428,928
36Sep 29, 2025 20:1357,46059,457,29631,308,0002,019,0002,387,968
37Sep 29, 2025 20:1357,73657,813,72030,443,0003,044,0002,355,200
38Sep 29, 2025 20:1458,16957,770,17930,762,0002,976,0002,486,272
39Sep 29, 2025 20:1359,10057,149,07831,254,0003,024,0002,486,272