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 19:46Wookyung JeongWookyung JeongScore: 61,253Success
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 19:4659,70263,871,96630,554,0004,073,0002,277,376
2Sep 27, 2025 19:4660,21758,389,08326,943,0007,983,0002,494,464
3Sep 27, 2025 19:4860,54561,087,41130,100,0005,016,0002,285,568
4Sep 27, 2025 19:4660,68462,678,57633,186,0002,011,0002,461,696
5Sep 27, 2025 19:4660,77656,171,37933,236,0002,014,0002,367,488
6Sep 27, 2025 19:4660,81462,285,17531,241,0004,031,0002,412,544
7Sep 27, 2025 19:4960,85056,781,60631,260,0004,033,0002,420,736
8Sep 27, 2025 19:4660,97159,982,55531,322,0004,041,0002,424,832
9Sep 27, 2025 19:4961,12256,766,49432,413,0003,038,0002,347,008
10Sep 27, 2025 19:4661,25357,245,62529,437,0006,090,0002,502,656
11Sep 27, 2025 19:4861,26758,662,42130,459,0005,076,0002,359,296
12Sep 27, 2025 19:4661,27160,272,39632,491,0003,046,0002,490,368
13Sep 27, 2025 19:4662,31258,842,45930,118,0006,023,0002,392,064
14Sep 27, 2025 19:4862,32861,094,18928,117,0008,033,0002,322,432
15Sep 27, 2025 19:4663,67965,217,46230,945,0005,989,0002,510,848
16Sep 27, 2025 19:4664,03160,650,94830,112,0007,026,0002,355,200
17Sep 27, 2025 19:4664,11760,052,34431,158,0006,030,0002,265,088
18Sep 27, 2025 19:4965,75558,561,29533,120,0005,018,0002,400,256