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 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 | |||
| Sep 29, 2023 15:31 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:55 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:52 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:46 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:41 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:27 | CPPg++9.3.0 | Error | |||
| Sep 29, 2023 14:25 | CPPg++9.3.0 | Success | 196,940+20.79 RP | ||
| Sep 28, 2023 23:44 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:26 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:24 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:14 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:11 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:10 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 23:06 | CPPg++9.3.0 | Success | 333,458+16.69 RP | ||
| Sep 28, 2023 22:33 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 22:33 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 22:32 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 22:29 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 22:03 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:54 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:48 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:43 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:41 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:40 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:35 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:35 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:33 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:32 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:30 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:29 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:27 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:25 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:10 | CPPg++9.3.0 | Success | 766,075 | ||
| Sep 28, 2023 21:09 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 21:09 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 20:43 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 20:41 | CPPg++9.3.0 | Error | |||
| Sep 28, 2023 18:17 | CPPg++9.3.0 | Success | 774,069 | ||
| Sep 28, 2023 17:58 | CPPg++9.3.0 | Success | 763,835 | ||
| Sep 28, 2023 17:55 | CPPg++9.3.0 | Success | 772,726 | ||
| Sep 28, 2023 17:50 | CPPg++9.3.0 | Success | 761,066 | ||
| Sep 27, 2023 21:48 | CPPg++9.3.0 | Success | 767,871 | ||
| Sep 27, 2023 21:48 | CPPg++9.3.0 | Success | 769,988 | ||
| Sep 27, 2023 21:46 | CPPg++9.3.0 | Success | 752,019 | ||
| Sep 27, 2023 21:33 | CPPg++9.3.0 | Success | 765,429 | ||
| Sep 27, 2023 21:24 | CPPg++9.3.0 | Error | |||
| Sep 3, 2023 19:58 | GOgo1.20.7 | Success | 1,588,975 | ||
| Sep 3, 2023 16:50 | GOgo1.20.7 | Success | 1,569,813 | ||
| Aug 28, 2023 18:17 | GOgo1.20.7 | Success | 1,410,691+7.09 RP | ||
| Apr 27, 2023 10:00 | CPPg++9.3.0 | Success | 89,519+8.47 RP | ||
| Apr 27, 2023 09:47 | CPPg++9.3.0 | Success | 96,867+1.67 RP | ||
| Apr 27, 2023 09:45 | CPPg++9.3.0 | Error | |||
| Apr 27, 2023 09:45 | CPPg++9.3.0 | Error | |||
| Apr 27, 2023 07:36 | CPPg++9.3.0 | Success | 98,464+3.13 RP | ||
| Apr 27, 2023 07:35 | CPPg++9.3.0 | Success | 2,045,593 |