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 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 | ||
| Sep 30, 2023 05:47 | CPPg++9.3.0 | Success | 204,099 | ||
| Sep 30, 2023 05:42 | CPPg++9.3.0 | Success | 214,659 | ||
| Sep 30, 2023 05:40 | CPPg++9.3.0 | Success | 242,256 | ||
| Sep 30, 2023 03:42 | CPPg++9.3.0 | Success | 319,552 | ||
| Sep 30, 2023 03:39 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 03:38 | CPPg++9.3.0 | Success | 361,428 | ||
| Sep 30, 2023 03:35 | CPPg++9.3.0 | Success | 494,243 | ||
| Sep 30, 2023 03:34 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 03:01 | CPPg++9.3.0 | Success | 355,780 | ||
| Sep 30, 2023 00:41 | CPPg++9.3.0 | Success | 349,372 | ||
| Sep 30, 2023 00:38 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 00:19 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 00:18 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 00:14 | CPPg++9.3.0 | Success | 331,704 | ||
| Sep 30, 2023 00:14 | CPPg++9.3.0 | Success | 335,461 | ||
| Sep 30, 2023 00:01 | CPPg++9.3.0 | Error | |||
| Sep 30, 2023 00:00 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 23:56 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 23:55 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 23:54 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 23:49 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:54 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:49 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:47 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:47 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:44 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:39 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:31 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 22:29 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:34 | CPPg++9.3.0 | Success | 344,046 | ||
| Sep 29, 2023 20:32 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:31 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:29 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:17 | CPPg++9.3.0 | Success | 219,758 | ||
| Sep 29, 2023 20:15 | CPPg++9.3.0 | Success | 242,581 | ||
| Sep 29, 2023 20:12 | CPPg++9.3.0 | Success | 270,823 | ||
| Sep 29, 2023 20:10 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:08 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:06 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:05 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 20:02 | CPPg++9.3.0 | Success | 296,907 | ||
| Sep 29, 2023 20:01 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 19:59 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 15:40 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 15:33 | CPPg++9.3.0 | Error |