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 28, 2025 18:24Josu San MartinJosu San MartinScore: 8,460Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 18:217,76623,635,8934,504,00002,514,944
2Dec 28, 2025 18:247,85923,185,9273,419,0001,139,0002,514,944
3Dec 28, 2025 18:257,93426,260,8823,452,0001,150,0002,469,888
4Dec 28, 2025 18:217,95923,176,4293,462,0001,154,0002,510,848
5Dec 28, 2025 18:217,99020,083,5013,476,0001,158,0002,482,176
6Dec 28, 2025 18:218,00718,623,9693,483,0001,161,0002,514,944
7Dec 28, 2025 18:218,10722,241,9114,702,00002,433,024
8Dec 28, 2025 18:248,45721,857,7203,924,000981,0002,387,968
9Dec 28, 2025 18:258,46027,358,0962,944,0001,963,0002,351,104
10Dec 28, 2025 18:248,46019,534,7203,926,000981,0002,387,968
11Dec 28, 2025 18:248,46026,847,5204,907,00002,527,232
12Dec 28, 2025 18:248,46720,997,7802,947,0001,964,0002,285,568
13Dec 28, 2025 18:218,47424,386,7263,932,000983,0002,482,176
14Dec 28, 2025 18:248,47620,746,5953,933,000983,0002,408,448
15Dec 28, 2025 18:248,48620,659,0812,953,0001,969,0002,514,944
16Dec 28, 2025 18:258,48617,540,4373,938,000984,0002,281,472
17Dec 28, 2025 18:218,51425,772,5662,963,0001,975,0002,392,064
18Dec 28, 2025 18:248,54521,907,5444,956,00002,461,696
19Dec 28, 2025 18:248,57225,422,9083,978,000994,0002,453,504
20Dec 28, 2025 18:218,57923,415,4272,986,0001,990,0002,293,760
21Dec 28, 2025 18:218,60521,128,2463,993,000998,0002,527,232