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 listJan 18, 2025 15:32AnSaAnSaError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 18, 2025 15:3000000Error: expected "566640 ", got "+ 1137 100 + 1130 10 + 1130 50 - 0 + 1150 200 = 200 2300 "
2Jan 18, 2025 15:321,701,4451,017,449,744985,839,000999,0002,281,472
3Jan 18, 2025 15:321,704,3021,019,719,024986,497,0001,998,0002,281,472
4Jan 18, 2025 15:321,706,4971,012,623,526989,768,00002,494,464
5Jan 18, 2025 15:321,706,8711,017,194,857988,986,000999,0002,490,368
6Jan 18, 2025 15:321,722,7001,025,226,005999,166,00002,486,272
7Jan 18, 2025 15:321,776,4841,059,387,2831,028,363,0001,998,0002,289,664
8Jan 18, 2025 15:321,793,2551,068,005,4111,039,088,0001,000,0002,277,376
9Jan 18, 2025 15:321,890,2091,122,196,0741,094,323,0001,998,0002,494,464
10Jan 18, 2025 15:321,914,0091,134,401,1071,108,127,0001,998,0002,293,760