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 14:54Wookyung JeongWookyung JeongScore: 60,578Success
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 14:5457,48457,775,11227,279,0006,062,0002,367,488
2Sep 29, 2025 14:5558,11464,440,23030,642,0003,064,0002,502,656
3Sep 29, 2025 14:5458,14061,800,98429,634,0004,087,0002,453,504
4Sep 29, 2025 14:5458,41261,132,79429,894,0003,985,0002,400,256
5Sep 29, 2025 14:5758,70957,341,03628,042,0006,009,0002,347,008
6Sep 29, 2025 14:5459,64355,948,82832,559,0002,034,0002,404,352
7Sep 29, 2025 14:5459,79761,111,06128,562,0006,120,0002,506,752
8Sep 29, 2025 14:5660,10362,091,77132,868,0001,992,0002,408,448
9Sep 29, 2025 14:5460,12465,158,14829,891,0004,981,0002,375,680
10Sep 29, 2025 14:5560,16761,612,72730,909,0003,988,0002,523,136
11Sep 29, 2025 14:5760,57859,717,41833,128,0002,007,0002,277,376
12Sep 29, 2025 14:5560,86757,989,85031,269,0004,034,0002,342,912
13Sep 29, 2025 14:5661,08168,701,04230,366,0005,061,0002,490,368
14Sep 29, 2025 14:5561,24759,186,48134,509,0001,014,0002,404,352
15Sep 29, 2025 14:5461,32462,694,00528,455,0007,113,0002,285,568
16Sep 29, 2025 14:5461,37861,983,85331,531,0004,068,0002,265,088
17Sep 29, 2025 14:5661,87962,025,61031,903,0003,987,0002,322,432
18Sep 29, 2025 14:5461,90062,422,29330,916,0004,986,0002,428,928
19Sep 29, 2025 14:5761,99367,895,81829,964,0005,992,0002,490,368
20Sep 29, 2025 14:5561,99756,907,17832,962,0002,996,0002,506,752
21Sep 29, 2025 14:5564,03365,241,98831,117,0006,022,0002,306,048