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.

Date AuthorLanguageStatus Score
Feb 15, 2024 19:08Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:08__RUSTrust-1.76.0Error
Feb 15, 2024 19:02__RUSTrust-1.76.0Error
Feb 15, 2024 19:00Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:00__RUSTrust-1.76.0Error
Feb 15, 2024 18:50Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 18:49Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 18:27__RUSTrust-1.76.0Error
Feb 15, 2024 18:24__RUSTrust-1.76.0Error
Feb 15, 2024 18:21__RUSTrust-1.76.0Error
Feb 15, 2024 18:19Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success2,171,102
Feb 15, 2024 18:05__RUSTrust-1.76.0Error
Feb 15, 2024 18:04Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success2,190,250
Feb 15, 2024 18:00__RUSTrust-1.76.0Error
Feb 15, 2024 17:54__RUSTrust-1.76.0Error
Feb 15, 2024 17:47__RUSTrust-1.76.0Error
Feb 15, 2024 17:45__RUSTrust-1.76.0Error
Feb 15, 2024 17:43Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success2,205,069
Feb 15, 2024 17:40__RUSTrust-1.76.0Error
Feb 15, 2024 17:40__RUSTrust-1.76.0Error
Feb 15, 2024 17:35__RUSTrust-1.76.0Error
Feb 15, 2024 17:33Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success2,305,313
Feb 15, 2024 17:30Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success2,201,189
Feb 15, 2024 17:20__RUSTrust-1.76.0Error
Feb 15, 2024 17:14__RUSTrust-1.76.0Error
Feb 15, 2024 17:10__RUSTrust-1.76.0Error
Feb 15, 2024 17:08__RUSTrust-1.76.0Error
Feb 15, 2024 17:02__RUSTrust-1.76.0Error
Feb 15, 2024 16:56__RUSTrust-1.76.0Error
Feb 15, 2024 16:49__RUSTrust-1.76.0Error
Feb 15, 2024 16:44__RUSTrust-1.76.0Error
Feb 15, 2024 16:43__RUSTrust-1.76.0Error
Feb 15, 2024 16:36Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,906,746+5.24 RP
Feb 15, 2024 16:34__RUSTrust-1.76.0Error
Feb 15, 2024 16:33__RUSTrust-1.76.0Error
Feb 15, 2024 16:32__RUSTrust-1.76.0Error
Feb 15, 2024 16:30__RUSTrust-1.76.0Error
Feb 15, 2024 16:24__RUSTrust-1.76.0Error
Feb 15, 2024 16:24__RUSTrust-1.76.0Error
Feb 15, 2024 16:21__RUSTrust-1.76.0Error
Feb 15, 2024 14:45__RUSTrust-1.76.0Error
Feb 15, 2024 14:45__RUSTrust-1.76.0Error
Feb 15, 2024 10:08__RUSTrust-1.76.0Error
Feb 14, 2024 20:56__RUSTrust-1.76.0Error
Feb 14, 2024 20:56__RUSTrust-1.74.0Error
Feb 14, 2024 20:38__RUSTrust-1.74.0Error
Feb 14, 2024 17:43__RUSTrust-1.74.0Error
Feb 14, 2024 17:39__RUSTrust-1.74.0Error
Feb 14, 2024 17:00__RUSTrust-1.74.0Error
Feb 14, 2024 16:45__RUSTrust-1.74.0Error
Feb 14, 2024 16:15__RUSTrust-1.74.0Error
Feb 14, 2024 16:03__RUSTrust-1.74.0Error
Feb 14, 2024 15:07__RUSTrust-1.74.0Error
Feb 14, 2024 14:39__RUSTrust-1.74.0Error
Feb 14, 2024 14:35__RUSTrust-1.74.0Error
Feb 14, 2024 14:33__RUSTrust-1.74.0Error
Feb 14, 2024 14:33__RUSTrust-1.74.0Error
Feb 14, 2024 01:23stdspstdspRUSTrust-1.76.0Success111,154
Feb 14, 2024 01:22stdspstdspRUSTrust-1.76.0Error
Feb 14, 2024 01:21stdspstdspRUSTrust-1.76.0Success112,279
Feb 14, 2024 01:19stdspstdspRUSTrust-1.76.0Success110,622+0.25 RP
Feb 14, 2024 01:16stdspstdspRUSTrust-1.76.0Success114,788
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,690
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,697
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,672
Feb 14, 2024 01:08stdspstdspRUSTrust-1.76.0Success379,183
Feb 14, 2024 01:01stdspstdspRUSTrust-1.76.0Success111,567
Feb 14, 2024 00:20stdspstdspRUSTrust-1.76.0Success114,397
Feb 13, 2024 23:51stdspstdspRUSTrust-1.76.0Success271,357
Feb 13, 2024 23:46stdspstdspRUSTrust-1.76.0Success112,131
Feb 13, 2024 23:45stdspstdspRUSTrust-1.76.0Success112,554
Feb 13, 2024 23:44stdspstdspRUSTrust-1.76.0Success114,333
Feb 13, 2024 23:38stdspstdspRUSTrust-1.76.0Success111,935
Feb 13, 2024 23:33stdspstdspRUSTrust-1.76.0Success112,588
Feb 13, 2024 23:31stdspstdspRUSTrust-1.76.0Success110,932+0.68 RP
Feb 13, 2024 23:30stdspstdspRUSTrust-1.76.0Success114,905
Feb 13, 2024 23:29stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:26stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:24stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:16stdspstdspRUSTrust-1.76.0Success111,780+0.64 RP
Feb 13, 2024 23:15stdspstdspRUSTrust-1.76.0Success112,580+4.71 RP
Feb 13, 2024 23:14stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:11stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:05stdspstdspRUSTrust-1.76.0Success120,870
Feb 13, 2024 23:01stdspstdspRUSTrust-1.76.0Success128,010
Feb 13, 2024 23:00stdspstdspRUSTrust-1.76.0Success119,980
Feb 13, 2024 22:59stdspstdspRUSTrust-1.75.0Error
Feb 13, 2024 22:09stdspstdspRUSTrust-1.75.0Success120,512
Feb 13, 2024 22:00stdspstdspRUSTrust-1.75.0Success118,883+0.32 RP
Feb 13, 2024 21:58stdspstdspRUSTrust-1.75.0Success121,476
Feb 13, 2024 20:10__RUSTrust-1.74.0Error
Feb 13, 2024 20:10__RUSTrust-1.74.0Error
Feb 13, 2024 20:08__RUSTrust-1.74.0Error
Feb 13, 2024 20:08__RUSTrust-1.74.0Error
Feb 13, 2024 20:07__RUSTrust-1.74.0Error
Feb 13, 2024 19:49__RUSTrust-1.74.0Error
Feb 13, 2024 19:47__RUSTrust-1.74.0Error
Feb 13, 2024 09:57__RUSTrust-1.74.0Error
Feb 13, 2024 09:52__RUSTrust-1.74.0Error
Feb 13, 2024 09:51__RUSTrust-1.74.0Error