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 14:36Wookyung JeongWookyung JeongScore: 86,928Success
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 14:3678,09172,135,84140,261,0005,032,0002,293,760
2Sep 27, 2025 14:3579,22273,973,73038,957,0006,992,0002,519,040
3Sep 27, 2025 14:3679,74368,726,63336,197,00010,054,0002,260,992
4Sep 27, 2025 14:3680,39174,325,66740,546,0006,081,0002,437,120
5Sep 27, 2025 14:3580,42977,195,40440,565,0006,084,0002,379,776
6Sep 27, 2025 14:3481,60373,569,68643,302,0004,028,0002,465,792
7Sep 27, 2025 14:3681,67474,242,97442,332,0005,039,0002,523,136
8Sep 27, 2025 14:3681,84572,652,00240,400,0007,070,0002,371,584
9Sep 27, 2025 14:3482,76669,777,88243,004,0005,000,0002,269,184
10Sep 27, 2025 14:3684,78672,063,85841,148,0008,028,0002,269,184
11Sep 27, 2025 14:3684,79168,921,88542,154,0007,025,0002,269,184
12Sep 27, 2025 14:3684,80270,765,07541,155,0008,030,0002,383,872
13Sep 27, 2025 14:3484,99076,346,49046,276,0003,018,0002,457,600
14Sep 27, 2025 14:3586,02479,294,86441,911,0007,983,0002,482,176
15Sep 27, 2025 14:3586,74174,861,71543,267,0007,043,0002,519,040
16Sep 27, 2025 14:3486,84073,339,07142,309,0008,058,0002,441,216
17Sep 27, 2025 14:3486,89779,750,85142,336,0008,064,0002,437,120
18Sep 27, 2025 14:3686,89876,545,58540,321,00010,080,0002,506,752
19Sep 27, 2025 14:3686,92876,334,64745,377,0005,041,0002,424,832
20Sep 27, 2025 14:3587,64876,933,06042,862,0007,974,0002,510,848
21Sep 27, 2025 14:3587,89374,352,15442,982,0007,996,0002,383,872
22Sep 27, 2025 14:3588,60077,617,40341,312,00010,076,0002,260,992
23Sep 27, 2025 14:3688,61973,458,16844,345,0007,054,0002,469,888
24Sep 27, 2025 14:3488,99382,547,62144,532,0007,084,0002,519,040
25Sep 27, 2025 14:3689,42274,708,85146,781,0005,084,0002,363,392
26Sep 27, 2025 14:3489,66275,736,81146,004,0006,000,0002,428,928
27Sep 27, 2025 14:3589,69775,434,06346,022,0006,002,0002,269,184
28Sep 27, 2025 14:3689,72474,842,00546,036,0006,004,0002,396,160
29Sep 27, 2025 14:3690,06675,788,96345,206,0007,032,0002,523,136
30Sep 27, 2025 14:3590,07678,530,30346,216,0006,028,0002,449,408
31Sep 27, 2025 14:3490,44577,553,62145,397,0007,061,0002,519,040
32Sep 27, 2025 14:3490,44779,765,83147,415,0005,044,0002,260,992
33Sep 27, 2025 14:3691,16781,504,32143,898,0008,979,0002,519,040
34Sep 27, 2025 14:3691,52980,103,40945,074,0008,013,0002,269,184
35Sep 27, 2025 14:3692,18475,970,24446,406,0007,061,0002,265,088
36Sep 27, 2025 14:3693,89880,603,32347,402,0007,059,0002,269,184