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 listMar 29, 2025 23:29Code HintsCode HintsScore: 109,159Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 29, 2025 23:29108,49589,390,45261,929,000998,0002,531,328
2Mar 29, 2025 23:29108,62190,708,11661,000,0002,000,0002,416,640
3Mar 29, 2025 23:23108,63689,492,85861,009,0002,000,0002,523,136
4Mar 29, 2025 23:29108,66987,300,13362,028,0001,000,0002,531,328
5Mar 29, 2025 23:29108,93189,480,65862,178,0001,002,0002,416,640
6Mar 29, 2025 23:23108,98883,955,44961,207,0002,006,0002,416,640
7Mar 29, 2025 23:23109,02885,208,61461,229,0002,007,0002,416,640
8Mar 29, 2025 23:23109,09787,805,63863,276,00002,535,424
9Mar 29, 2025 23:23109,13890,670,67663,300,00002,269,184
10Mar 29, 2025 23:29109,15986,025,47662,308,0001,004,0002,535,424
11Mar 29, 2025 23:23109,18191,392,83062,320,0001,005,0002,424,832
12Mar 29, 2025 23:23109,20584,462,84263,339,00002,420,736
13Mar 29, 2025 23:29109,30786,878,18463,398,00002,424,832
14Mar 29, 2025 23:23109,31688,665,75862,397,0001,006,0002,535,424
15Mar 29, 2025 23:29109,32897,122,36163,410,00002,514,944
16Mar 29, 2025 23:29109,37187,003,72562,429,0001,006,0002,535,424
17Mar 29, 2025 23:29109,57184,492,93963,551,00002,531,328
18Mar 29, 2025 23:23109,69194,262,07263,621,00002,416,640