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 11, 2025 16:22caandocaandoScore: 139,905Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2025 16:28126,78696,053,07470,514,0003,022,0002,506,752
2Sep 11, 2025 16:28130,07898,861,86171,422,0004,023,0002,371,584
3Sep 11, 2025 16:27130,490102,535,72973,666,0002,018,0002,338,816
4Sep 11, 2025 16:27131,03197,041,84571,999,0003,999,0002,506,752
5Sep 11, 2025 16:27131,37899,756,48571,186,0005,013,0002,293,760
6Sep 11, 2025 16:28131,740102,903,99672,388,0004,021,0002,322,432
7Sep 11, 2025 16:28132,505106,557,59574,857,0001,996,0002,408,448
8Sep 11, 2025 16:28132,70597,732,70473,971,0002,998,0002,326,528
9Sep 11, 2025 16:28133,464102,144,15170,372,0007,037,0002,273,280
10Sep 11, 2025 16:28133,46798,835,73874,395,0003,016,0002,433,024
11Sep 11, 2025 16:28133,695100,526,46873,515,0004,028,0002,355,200
12Sep 11, 2025 16:28134,421101,874,26273,966,0003,998,0002,490,368
13Sep 11, 2025 16:27134,72995,345,16173,134,0005,009,0002,306,048
14Sep 11, 2025 16:28134,829111,192,68472,186,0006,015,0002,424,832
15Sep 11, 2025 16:28135,091101,802,26177,349,0001,004,0002,428,928
16Sep 11, 2025 16:27135,152106,786,88373,364,0005,024,0002,326,528
17Sep 11, 2025 16:27135,700111,549,98271,643,0007,063,0002,371,584
18Sep 11, 2025 16:28138,712102,581,96677,436,0003,017,0002,387,968
19Sep 11, 2025 16:25139,317104,039,09378,809,0001,995,0002,408,448
20Sep 11, 2025 16:27139,905110,255,37478,140,0003,005,0002,482,176
21Sep 11, 2025 16:25141,355112,178,78572,988,0008,998,0002,502,656
22Sep 11, 2025 16:25141,409107,375,42977,016,0005,001,0002,338,816
23Sep 11, 2025 16:27143,443111,478,40975,178,0008,019,0002,363,392
24Sep 11, 2025 16:25143,872115,914,05978,420,0005,026,0002,371,584
25Sep 11, 2025 16:27143,988105,713,38074,458,0009,055,0002,469,888
26Sep 11, 2025 16:22145,629110,490,92279,438,0005,027,0002,265,088
27Sep 11, 2025 16:22145,660108,551,12981,466,0003,017,0002,330,624
28Sep 11, 2025 16:25146,459113,435,59379,950,0004,996,0002,301,952
29Sep 11, 2025 16:25147,286109,493,07577,386,0008,040,0002,297,856
30Sep 11, 2025 16:25147,712110,599,93281,642,0004,031,0002,301,952
31Sep 11, 2025 16:22148,424110,768,14880,080,0006,006,0002,412,544
32Sep 11, 2025 16:22148,660112,861,97680,208,0006,015,0002,478,080
33Sep 11, 2025 16:25149,372115,414,99479,585,0007,051,0002,412,544
34Sep 11, 2025 16:22150,643117,052,11679,339,0008,034,0002,510,848
35Sep 11, 2025 16:22151,660114,031,93982,966,0004,997,0002,424,832
36Sep 11, 2025 16:22152,302116,645,68880,305,0008,030,0002,314,240
37Sep 11, 2025 16:22152,391117,010,73282,361,0006,026,0002,396,160
38Sep 11, 2025 16:25156,788117,033,21785,941,0004,996,0002,318,336
39Sep 11, 2025 16:22157,140119,327,97984,131,0007,010,0002,334,720