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 listDec 18, 2025 15:52Wookyung JeongWookyung JeongScore: 37,738Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 18, 2025 15:5336,39842,488,48819,101,0002,010,0002,433,024
2Dec 18, 2025 15:5236,89850,941,49919,363,0002,038,0002,502,656
3Dec 18, 2025 15:5336,97146,507,90720,422,0001,021,0002,416,640
4Dec 18, 2025 15:5236,98842,527,13520,432,0001,021,0002,506,752
5Dec 18, 2025 15:5237,30255,056,63421,635,00002,461,696
6Dec 18, 2025 15:5237,39749,449,50420,658,0001,032,0002,355,200
7Dec 18, 2025 15:5337,44148,865,10820,682,0001,034,0002,334,720
8Dec 18, 2025 15:5337,68149,195,62021,855,00002,502,656
9Dec 18, 2025 15:5337,73846,822,79119,899,0001,989,0002,392,064
10Dec 18, 2025 15:5337,73845,906,09319,899,0001,989,0002,469,888
11Dec 18, 2025 15:5237,74846,695,46119,904,0001,990,0002,424,832
12Dec 18, 2025 15:5337,75251,397,02820,901,000995,0002,449,408
13Dec 18, 2025 15:5237,76449,946,92019,912,0001,991,0002,461,696
14Dec 18, 2025 15:5337,76949,975,21920,911,000995,0002,379,776
15Dec 18, 2025 15:5237,77952,874,69420,916,000996,0002,465,792
16Dec 18, 2025 15:5237,94747,955,32920,009,0002,000,0002,433,024
17Dec 18, 2025 15:5337,99346,007,60720,033,0002,003,0002,301,952
18Dec 18, 2025 15:5238,06946,668,14420,073,0002,007,0002,437,120