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 listApr 7, 2025 16:01Bernard TeoBernard TeoScore: 1,243,905Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 7, 2025 16:021,207,531731,999,946697,367,0003,001,0002,277,376
2Apr 7, 2025 16:021,224,360734,293,499709,129,0001,000,0002,273,280
3Apr 7, 2025 16:021,224,778738,843,228708,370,0002,001,0002,273,280
4Apr 7, 2025 16:021,225,495739,887,180706,789,0003,998,0002,273,280
5Apr 7, 2025 16:021,231,166740,639,190713,076,0001,000,0002,281,472
6Apr 7, 2025 16:011,232,995740,779,046712,137,0003,000,0002,498,560
7Apr 7, 2025 16:011,242,022743,956,955718,375,0001,998,0002,269,184
8Apr 7, 2025 16:021,243,905746,150,150717,468,0003,997,0002,281,472
9Apr 7, 2025 16:021,246,178744,366,026719,784,0002,999,0002,277,376
10Apr 7, 2025 16:011,247,059757,834,532722,294,0001,000,0002,281,472
11Apr 7, 2025 16:011,251,848748,338,050725,072,0001,000,0002,277,376
12Apr 7, 2025 16:011,251,890751,080,285724,099,0001,997,0002,277,376
13Apr 7, 2025 16:011,258,976759,437,276728,209,0001,997,0002,273,280
14Apr 7, 2025 16:021,259,826758,982,775724,702,0005,997,0002,273,280
15Apr 7, 2025 16:021,264,886762,168,931729,636,0003,998,0002,490,368