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 16:15Wookyung JeongWookyung JeongScore: 78,078Success
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 16:1571,18461,577,47632,224,0009,063,0002,510,848
2Sep 27, 2025 16:1672,45971,056,43638,024,0004,002,0002,342,912
3Sep 27, 2025 16:1672,50068,906,56439,047,0003,003,0002,371,584
4Sep 27, 2025 16:1673,93168,746,46136,897,0005,983,0002,396,160
5Sep 27, 2025 16:1674,94369,859,26337,402,0006,065,0002,453,504
6Sep 27, 2025 16:1677,95366,639,53440,190,0005,023,0002,523,136
7Sep 27, 2025 16:1678,07870,979,23941,260,0004,025,0002,293,760
8Sep 27, 2025 16:1680,40078,007,95740,550,0006,082,0002,363,392
9Sep 27, 2025 16:1580,43177,261,40241,580,0005,070,0002,301,952
10Sep 27, 2025 16:1680,82476,241,74338,899,0007,979,0002,306,048
11Sep 27, 2025 16:1580,86778,936,69441,805,0005,098,0002,465,792
12Sep 27, 2025 16:1681,27269,307,64241,121,0006,017,0002,396,160