Order book 
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 | Author | Language | Status | Score | |
|---|---|---|---|---|---|
| Feb 18, 2024 04:48 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:45 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:41 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:40 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:36 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:32 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:30 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:28 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:28 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:24 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 04:22 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 04:22 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 04:16 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 04:12 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 04:12 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 03:36 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 03:31 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 00:40 | CPPclang++10.0.0 | Error | |||
| Feb 18, 2024 00:40 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 15:28 | RUSTrust-1.74.0 | Success | 93,031+32.42 RP | ||
| Feb 17, 2024 10:35 | CPPclang++10.0.0 | Success | 2,330,804+4.29 RP | ||
| Feb 17, 2024 06:57 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:54 | CPPclang++10.0.0 | Success | 33,036+92.01 RP | ||
| Feb 17, 2024 06:54 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:52 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:52 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:49 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:48 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:47 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:44 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:44 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:42 | CPPclang++10.0.0 | Error | |||
| Feb 17, 2024 06:20 | CPPg++9.4.0 | Error | |||
| Feb 17, 2024 06:19 | CPPg++9.4.0 | Error | |||
| Feb 17, 2024 06:19 | CPPg++9.4.0 | Success | 2,242,919 | ||
| Feb 17, 2024 00:53 | CPPg++9.4.0 | Error | |||
| Feb 17, 2024 00:52 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 21:09 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 21:08 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 21:07 | CPPg++9.4.0 | Success | 67,140 | ||
| Feb 16, 2024 21:06 | CPPg++9.4.0 | Success | 68,216 | ||
| Feb 16, 2024 21:06 | CPPg++9.4.0 | Success | 68,188 | ||
| Feb 16, 2024 21:06 | CPPg++9.4.0 | Success | 81,449 | ||
| Feb 16, 2024 21:05 | CPPg++9.4.0 | Success | 68,439 | ||
| Feb 16, 2024 21:05 | CPPg++9.4.0 | Success | 99,831 | ||
| Feb 16, 2024 21:04 | CPPclang++10.0.0 | Success | 148,748 | ||
| Feb 16, 2024 21:04 | CPPg++9.4.0 | Success | 68,175 | ||
| Feb 16, 2024 21:02 | CPPg++9.4.0 | Success | 68,753 | ||
| Feb 16, 2024 20:58 | CPPg++9.4.0 | Success | 75,383 | ||
| Feb 16, 2024 20:58 | CPPg++9.4.0 | Success | 75,690 | ||
| Feb 16, 2024 20:52 | CPPg++9.4.0 | Success | 68,241 | ||
| Feb 16, 2024 20:49 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:48 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:47 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:46 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:46 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:42 | CPPg++9.4.0 | Success | 46,787 | ||
| Feb 16, 2024 20:39 | CPPg++9.4.0 | Success | 46,007 | ||
| Feb 16, 2024 20:36 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:34 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:34 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:33 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:33 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:31 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:30 | CPPg++9.4.0 | Success | 38,276 | ||
| Feb 16, 2024 20:30 | CPPg++9.4.0 | Success | 44,845 | ||
| Feb 16, 2024 20:27 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:24 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:22 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:20 | CPPg++9.4.0 | Success | 43,136 | ||
| Feb 16, 2024 20:19 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:14 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 20:13 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 19:57 | CPPg++9.4.0 | Success | 39,447 | ||
| Feb 16, 2024 19:55 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 19:44 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 19:43 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 19:42 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 19:41 | CPPg++9.4.0 | Success | 39,289 | ||
| Feb 16, 2024 19:40 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:55 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:46 | CPPg++9.4.0 | Success | 118,453 | ||
| Feb 16, 2024 17:45 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:38 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:37 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:35 | CPPg++9.4.0 | Success | 2,897,360 | ||
| Feb 16, 2024 17:34 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:30 | CPPg++9.4.0 | Success | 118,671 | ||
| Feb 16, 2024 17:30 | RUSTrust-1.74.0 | Success | 151,652 | ||
| Feb 16, 2024 17:30 | CPPg++9.4.0 | Success | 118,517 | ||
| Feb 16, 2024 17:29 | CPPg++9.4.0 | Success | 118,588 | ||
| Feb 16, 2024 17:29 | CPPg++9.4.0 | Success | 116,523 | ||
| Feb 16, 2024 17:24 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:22 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 17:15 | RUSTrust-1.74.0 | Error | |||
| Feb 16, 2024 17:14 | RUSTrust-1.74.0 | Success | 133,211+31.64 RP | ||
| Feb 16, 2024 16:39 | CPPg++9.4.0 | Error | |||
| Feb 16, 2024 13:41 | CPPclang++10.0.0 | Error | |||
| Feb 16, 2024 13:41 | CPPclang++10.0.0 | Error | |||
| Feb 16, 2024 10:05 | CPPg++9.4.0 | Error |