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 15:09Wookyung JeongWookyung JeongScore: 60,141Success
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 15:0955,10354,127,69627,965,0003,995,0002,347,008
2Sep 29, 2025 15:0055,67155,136,75428,253,0004,036,0002,441,216
3Sep 29, 2025 14:5955,99754,413,77327,404,0005,074,0002,269,184
4Sep 29, 2025 15:0156,92865,546,64530,017,0003,001,0002,482,176
5Sep 29, 2025 14:5957,08655,180,45228,094,0005,016,0002,408,448
6Sep 29, 2025 15:0957,16957,356,09130,144,0003,014,0002,506,752
7Sep 29, 2025 14:5957,54754,105,43929,332,0004,045,0002,428,928
8Sep 29, 2025 15:0958,41263,526,74727,901,0005,978,0002,379,776
9Sep 29, 2025 14:5958,42659,982,60729,901,0003,986,0002,293,760
10Sep 29, 2025 14:5958,59857,812,58629,989,0003,998,0002,379,776
11Sep 29, 2025 14:5958,62957,307,68629,005,0005,000,0002,408,448
12Sep 29, 2025 15:0059,70060,460,51327,497,0007,129,0002,367,488
13Sep 29, 2025 14:5960,14163,731,59531,893,0002,989,0002,265,088
14Sep 29, 2025 15:0060,14761,837,16828,905,0005,980,0002,330,624
15Sep 29, 2025 14:5960,15260,912,54829,904,0004,984,0002,428,928
16Sep 29, 2025 14:5960,28458,231,57030,969,0003,996,0002,375,680
17Sep 29, 2025 15:0160,31956,984,56727,988,0006,997,0002,314,240
18Sep 29, 2025 14:5960,33460,026,48431,995,0002,999,0002,412,544
19Sep 29, 2025 15:0960,60057,767,99932,136,0003,012,0002,277,376
20Sep 29, 2025 14:5960,66459,465,20131,164,0004,021,0002,482,176
21Sep 29, 2025 14:5960,93863,019,40329,285,0006,059,0002,510,848
22Sep 29, 2025 15:0961,52464,324,22432,626,0003,058,0002,301,952
23Sep 29, 2025 15:0961,89863,087,24830,915,0004,986,0002,310,144
24Sep 29, 2025 15:0164,26760,741,75731,231,0006,044,0002,293,760