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 | |
|---|---|---|---|---|---|
| Mar 25, 2024 12:50 | CPPg++9.4.0 | Error | |||
| Mar 25, 2024 12:48 | CPPg++9.4.0 | Error | |||
| Mar 25, 2024 12:33 | CPPg++9.4.0 | Success | 2,650,100 | ||
| Mar 25, 2024 12:32 | CPPg++9.4.0 | Success | 2,252,747 | ||
| Mar 25, 2024 12:10 | CPPg++9.4.0 | Success | 2,246,905+4.45 RP | ||
| Mar 22, 2024 13:08 | CPPg++9.4.0 | Success | 1,531,141 | ||
| Mar 22, 2024 13:04 | CPPg++9.4.0 | Success | 7,353,376 | ||
| Mar 22, 2024 13:02 | CPPg++9.4.0 | Success | 1,403,822 | ||
| Mar 22, 2024 13:02 | CPPg++9.4.0 | Success | 1,354,151 | ||
| Mar 22, 2024 13:01 | CPPg++9.4.0 | Error | |||
| Mar 22, 2024 12:57 | CPPg++9.4.0 | Success | 1,319,167+0.12 RP | ||
| Mar 22, 2024 12:56 | CPPg++9.4.0 | Success | 1,339,798+2.27 RP | ||
| Mar 22, 2024 12:55 | CPPg++9.4.0 | Error | |||
| Mar 22, 2024 12:54 | CPPg++9.4.0 | Error | |||
| Mar 22, 2024 12:53 | CPPg++9.4.0 | Error | |||
| Mar 22, 2024 12:43 | CPPg++9.4.0 | Success | 1,924,127+0.76 RP | ||
| Mar 22, 2024 12:42 | CPPg++9.4.0 | Error | |||
| Mar 22, 2024 12:37 | CPPclang++10.0.0 | Error | |||
| Mar 22, 2024 12:19 | CPPclang++10.0.0 | Success | 2,324,408 | ||
| Mar 22, 2024 12:19 | CPPg++9.4.0 | Success | 2,255,289 | ||
| Mar 22, 2024 12:18 | CPPg++9.4.0 | Success | 2,470,335 | ||
| Mar 22, 2024 12:17 | CPPg++9.4.0 | Success | 2,257,138+4.43 RP | ||
| Mar 10, 2024 23:18 | GOgo1.22.1 | Success | 3,284,663 | ||
| Mar 10, 2024 04:52 | GOgo1.22.1 | Success | 1,374,333+7.28 RP | ||
| Mar 8, 2024 04:30 | CPPg++9.4.0 | Success | 2,240,609+3.84 RP | ||
| Mar 7, 2024 13:24 | CPPg++9.4.0 | Error | |||
| Mar 7, 2024 13:22 | CPPg++9.4.0 | Success | 2,243,243+4.46 RP | ||
| Feb 20, 2024 19:29 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 19:23 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 19:17 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 18:22 | RUSTrust-1.76.0 | Success | 113,348 | ||
| Feb 20, 2024 18:21 | RUSTrust-1.76.0 | Success | 111,904 | ||
| Feb 20, 2024 18:19 | RUSTrust-1.76.0 | Success | 112,392 | ||
| Feb 20, 2024 18:05 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:59 | RUSTrust-1.76.0 | Success | 111,846 | ||
| Feb 20, 2024 17:56 | RUSTrust-1.76.0 | Success | 112,097 | ||
| Feb 20, 2024 17:55 | RUSTrust-1.74.0 | Success | 164,365 | ||
| Feb 20, 2024 17:54 | RUSTrust-1.76.0 | Success | 114,734 | ||
| Feb 20, 2024 17:53 | RUSTrust-1.76.0 | Success | 154,685 | ||
| Feb 20, 2024 17:53 | RUSTrust-1.76.0 | Success | 151,421 | ||
| Feb 20, 2024 17:53 | RUSTrust-1.76.0 | Success | 113,413 | ||
| Feb 20, 2024 17:51 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:49 | RUSTrust-1.76.0 | Success | 199,795 | ||
| Feb 20, 2024 17:48 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:46 | RUSTrust-1.76.0 | Success | 109,726 | ||
| Feb 20, 2024 17:45 | RUSTrust-1.76.0 | Success | 152,665 | ||
| Feb 20, 2024 17:43 | RUSTrust-1.76.0 | Success | 151,114 | ||
| Feb 20, 2024 17:42 | RUSTrust-1.76.0 | Success | 114,414 | ||
| Feb 20, 2024 17:40 | RUSTrust-1.76.0 | Success | 112,430 | ||
| Feb 20, 2024 17:38 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:35 | RUSTrust-1.76.0 | Success | 112,089 | ||
| Feb 20, 2024 17:34 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:33 | RUSTrust-1.76.0 | Success | 106,867 | ||
| Feb 20, 2024 17:32 | RUSTrust-1.76.0 | Success | 113,139 | ||
| Feb 20, 2024 17:29 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:22 | RUSTrust-1.74.0 | Success | 113,359+88.22 RP | ||
| Feb 20, 2024 17:22 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 17:18 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 16:58 | RUSTrust-1.76.0 | Success | 105,940+3.97 RP | ||
| Feb 20, 2024 16:48 | RUSTrust-1.76.0 | Success | 112,136 | ||
| Feb 20, 2024 16:47 | RUSTrust-1.76.0 | Error | |||
| Feb 20, 2024 16:47 | RUSTrust-1.76.0 | Success | 110,595+0.02 RP | ||
| Feb 20, 2024 16:44 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:25 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:25 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:24 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:23 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:20 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:18 | RUSTrust-1.76.0 | Error | |||
| Feb 19, 2024 12:15 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 19:33 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 19:32 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 19:02 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 18:56 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 18:48 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 18:32 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 18:07 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 17:55 | RUSTrust-1.76.0 | Success | 3,238,216 | ||
| Feb 18, 2024 17:54 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 17:08 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 17:04 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 17:00 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 14:11 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 14:10 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 14:09 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 14:07 | CPPg++9.4.0 | Success | 841,923+7.59 RP | ||
| Feb 18, 2024 14:02 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 14:00 | RUSTrust-1.76.0 | Success | 3,343,470 | ||
| Feb 18, 2024 13:59 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 13:57 | RUSTrust-1.76.0 | Error | |||
| Feb 18, 2024 12:52 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 12:47 | CPPg++9.4.0 | Success | 7,033,101 | ||
| Feb 18, 2024 11:40 | CPPg++9.4.0 | Success | 6,974,195 | ||
| Feb 18, 2024 05:23 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 05:21 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 05:20 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 05:17 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 05:15 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:51 | CPPg++9.4.0 | Error | |||
| Feb 18, 2024 04:50 | CPPg++9.4.0 | Error |