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 | |
|---|---|---|---|---|---|
| Sep 30, 2023 15:03 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 15:00 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:57 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:54 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:53 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:51 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:32 | CPPg++9.3.0 | Success | 127,813 | ||
| Sep 30, 2023 14:29 | CPPg++9.3.0 | Success | 126,017 | ||
| Sep 30, 2023 14:24 | CPPg++9.3.0 | Success | 126,941 | ||
| Sep 30, 2023 14:22 | CPPg++9.3.0 | Success | 125,620 | ||
| Sep 30, 2023 14:21 | CPPg++9.3.0 | Success | 127,165 | ||
| Sep 30, 2023 14:13 | CPPg++9.3.0 | Success | 126,862 | ||
| Sep 30, 2023 14:12 | CPPg++9.3.0 | Success | 126,092 | ||
| Sep 30, 2023 14:11 | CPPg++9.3.0 | Success | 124,067 | ||
| Sep 30, 2023 14:10 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:09 | CPPg++9.3.0 | Success | 123,656+0.25 RP | ||
| Sep 30, 2023 14:08 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:04 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 14:01 | CPPg++9.3.0 | Success | 127,898 | ||
| Sep 30, 2023 14:01 | CPPg++9.3.0 | Success | 124,041+0.14 RP | ||
| Sep 30, 2023 13:57 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 13:50 | CPPg++9.3.0 | Success | 177,905 | ||
| Sep 30, 2023 13:49 | CPPg++9.3.0 | Success | 177,363 | ||
| Sep 30, 2023 13:48 | CPPg++9.3.0 | Success | 126,917 | ||
| Sep 30, 2023 13:44 | CPPg++9.3.0 | Success | 127,898 | ||
| Sep 30, 2023 13:40 | CPPg++9.3.0 | Success | 135,677 | ||
| Sep 30, 2023 13:39 | CPPg++9.3.0 | Success | 134,366 | ||
| Sep 30, 2023 13:39 | CPPg++9.3.0 | Success | 135,533 | ||
| Sep 30, 2023 11:00 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:54 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:46 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:40 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:38 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:36 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:20 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:17 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:16 | CPPg++9.3.0 | Success | 124,907 | ||
| Sep 30, 2023 10:15 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:14 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:09 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:08 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 10:07 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 09:54 | CPPg++9.3.0 | Success | 141,611 | ||
| Sep 30, 2023 09:53 | CPPg++9.3.0 | Success | 351,966 | ||
| Sep 30, 2023 09:24 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 09:21 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 09:09 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 09:07 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:50 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:47 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:46 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:43 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:40 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:34 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:24 | CPPg++9.3.0 | Success | 124,262+0.37 RP | ||
| Sep 30, 2023 08:23 | CPPg++9.3.0 | Success | 125,152 | ||
| Sep 30, 2023 08:22 | CPPg++9.3.0 | Success | 133,451 | ||
| Sep 30, 2023 08:20 | CPPg++9.3.0 | Success | 125,347 | ||
| Sep 30, 2023 08:18 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 08:17 | CPPg++9.3.0 | Success | 124,836+2.91 RP | ||
| Sep 30, 2023 08:07 | CPPg++9.3.0 | Success | 131,007 | ||
| Sep 30, 2023 07:52 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:39 | CPPg++9.3.0 | Success | 129,543+1.28 RP | ||
| Sep 30, 2023 07:37 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:34 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:31 | CPPg++9.3.0 | Success | 131,721+0.36 RP | ||
| Sep 30, 2023 07:27 | CPPg++9.3.0 | Success | 132,349+0.56 RP | ||
| Sep 30, 2023 07:21 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:19 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:18 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:17 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:17 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:17 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:16 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:16 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:15 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 07:08 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:49 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:44 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:41 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:40 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:37 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:36 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:31 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:30 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:27 | CPPg++9.3.0 | Success | 133,334+4.20 RP | ||
| Sep 30, 2023 06:26 | CPPg++9.3.0 | Success | 156,118 | ||
| Sep 30, 2023 06:25 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 06:25 | CPPg++9.3.0 | Success | 174,698 | ||
| Sep 30, 2023 06:25 | CPPg++9.3.0 | Success | 162,797 | ||
| Sep 30, 2023 06:17 | CPPg++9.3.0 | Success | 141,240+5.14 RP | ||
| Sep 30, 2023 06:11 | CPPg++9.3.0 | Success | 152,307+7.37 RP | ||
| Sep 30, 2023 06:08 | CPPg++9.3.0 | Success | 172,167 | ||
| Sep 30, 2023 06:06 | CPPg++9.3.0 | Success | 224,885 | ||
| Sep 30, 2023 06:06 | CPPg++9.3.0 | Success | 199,531 | ||
| Sep 30, 2023 06:05 | CPPg++9.3.0 | Success | 172,572 | ||
| Sep 30, 2023 06:05 | CPPg++9.3.0 | Success | 171,560+1.38 RP | ||
| Sep 30, 2023 06:05 | CPPg++9.3.0 | Success | 175,719+6.13 RP | ||
| Sep 30, 2023 06:00 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 05:47 | CPPg++9.3.0 | Success | 200,166 |