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 listNov 7, 2024 11:42NoSIMD_C#NoSIMD_C#Score: 2,409,014Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 7, 2024 11:422,313,6791,371,546,5221,339,936,0001,998,0002,281,472
2Nov 7, 2024 11:422,313,8031,373,085,8561,341,006,0001,000,0002,289,664
3Nov 7, 2024 11:422,321,5981,376,098,6371,345,528,000999,0002,486,272
4Nov 7, 2024 11:422,393,1721,415,077,0261,387,041,000999,0002,281,472
5Nov 7, 2024 11:422,409,0141,425,245,7951,395,230,0001,998,0002,269,184
6Nov 7, 2024 11:422,436,1711,442,335,7531,408,982,0003,997,0002,281,472
7Nov 7, 2024 11:422,507,1971,482,083,8421,450,180,0003,994,0002,281,472
8Nov 7, 2024 11:422,567,0311,519,087,3931,487,879,000999,0002,482,176
9Nov 7, 2024 11:422,676,7241,575,917,9701,547,502,0004,998,0002,281,472