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 27, 2025 15:20Wookyung JeongWookyung JeongScore: 83,055Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 15:1975,66468,196,50236,904,0006,981,0002,416,640
2Sep 27, 2025 15:1976,44768,543,24139,301,0005,038,0002,490,368
3Sep 27, 2025 15:1976,99770,622,05540,599,0004,059,0002,379,776
4Sep 27, 2025 15:1977,40371,085,32940,904,0003,990,0002,437,120
5Sep 27, 2025 15:1778,40568,334,71739,412,0006,063,0002,469,888
6Sep 27, 2025 15:1779,31969,072,65744,005,0002,000,0002,433,024
7Sep 27, 2025 15:2079,40372,981,79640,047,0006,007,0002,408,448
8Sep 27, 2025 15:1980,58375,583,39343,690,0003,048,0002,318,336
9Sep 27, 2025 15:1980,77674,659,81041,866,0004,984,0002,314,240
10Sep 27, 2025 15:1980,87869,983,56740,791,0006,118,0002,285,568
11Sep 27, 2025 15:1881,12669,861,45142,048,0005,005,0002,424,832
12Sep 27, 2025 15:1981,90971,037,22239,421,0008,086,0002,334,720
13Sep 27, 2025 15:1881,99574,074,75736,427,00011,130,0002,334,720
14Sep 27, 2025 15:1782,58671,428,43341,913,0005,987,0002,334,720
15Sep 27, 2025 15:1983,01069,692,43942,128,0006,018,0002,387,968
16Sep 27, 2025 15:1983,05568,796,80941,147,0007,025,0002,367,488
17Sep 27, 2025 15:1783,54772,194,92842,400,0006,057,0002,457,600
18Sep 27, 2025 15:1983,83177,101,36338,493,00010,129,0002,424,832
19Sep 27, 2025 15:1983,86073,256,68940,533,0008,106,0002,387,968
20Sep 27, 2025 15:1784,15069,804,56341,835,0006,972,0002,260,992
21Sep 27, 2025 15:1984,27872,351,10542,896,0005,985,0002,482,176
22Sep 27, 2025 15:1984,33173,256,61340,927,0007,985,0002,486,272
23Sep 27, 2025 15:1984,34170,957,05441,930,0006,988,0002,387,968
24Sep 27, 2025 15:1885,53371,502,46345,560,0004,049,0002,371,584
25Sep 27, 2025 15:1985,74774,953,41741,614,0008,119,0002,461,696
26Sep 27, 2025 15:1986,81470,007,48243,303,0007,049,0002,510,848
27Sep 27, 2025 15:2087,18377,174,57548,544,0002,022,0002,301,952
28Sep 27, 2025 15:1787,87677,037,30343,973,0006,995,0002,392,064
29Sep 27, 2025 15:2088,01078,101,95246,042,0005,004,0002,269,184
30Sep 27, 2025 15:1991,67274,827,90047,151,0006,019,0002,510,848