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 | |
|---|---|---|---|---|---|
| Oct 15, 2024 16:29 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:28 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:28 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:26 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:26 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:24 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:23 | CPPclang++18.1.3 | Error | |||
| Oct 15, 2024 16:23 | CPPclang++18.1.3 | Error | |||
| Oct 13, 2024 22:05 | RUSTrust-1.81.0 | Success | 136,943 | ||
| Oct 4, 2024 08:11 | RUSTrust-1.76.0 | Error | |||
| Sep 12, 2024 12:22 | CPPclang++18.1.3 | Error | |||
| Sep 12, 2024 12:22 | CPPclang++18.1.3 | Error | |||
| Sep 12, 2024 12:20 | CPPclang++18.1.3 | Error | |||
| Sep 12, 2024 12:19 | CPPclang++18.1.3 | Error | |||
| Sep 10, 2024 17:00 | CPPclang++18.1.3 | Error | |||
| Sep 10, 2024 16:58 | CPPclang++18.1.3 | Error | |||
| Sep 10, 2024 16:57 | CPPclang++18.1.3 | Error | |||
| Sep 4, 2024 13:34 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:33 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:27 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:25 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:22 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:19 | CPPg++13.2.0 | Error | |||
| Sep 4, 2024 13:17 | CPPclang++18.1.3 | Error | |||
| Sep 4, 2024 13:16 | CPPg++13.2.0 | Success | 39,014 | ||
| Sep 3, 2024 16:46 | CPPclang++18.1.3 | Success | 2,371,024 | ||
| Sep 3, 2024 16:45 | CPPg++13.2.0 | Success | 2,437,757 | ||
| Aug 31, 2024 22:08 | CPPg++13.2.0 | Error | |||
| Aug 31, 2024 22:08 | CPPclang++18.1.3 | Error | |||
| Aug 31, 2024 22:07 | CPPg++13.2.0 | Error | |||
| Aug 31, 2024 22:06 | CPPg++13.2.0 | Error | |||
| Aug 3, 2024 00:12 | CPPg++9.4.0 | Success | 2,243,969+4.46 RP | ||
| Jul 24, 2024 22:07 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 22:05 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 22:04 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 22:03 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 22:02 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 22:02 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 21:58 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 21:57 | RUSTrust-1.79.0 | Error | |||
| Jul 24, 2024 20:33 | RUSTrust-1.79.0 | Success | 112,553 | ||
| Jul 3, 2024 17:15 | CPPclang++10.0.0 | Error | |||
| Jul 3, 2024 15:44 | CPPclang++10.0.0 | Error | |||
| Jul 3, 2024 15:38 | CPPclang++10.0.0 | Error | |||
| Jul 3, 2024 15:13 | CPPclang++10.0.0 | Success | 77,847 | ||
| Jul 3, 2024 11:35 | GOgo1.22.5 | Success | 1,368,127 | ||
| Jun 24, 2024 16:34 | CPPclang++10.0.0 | Error | |||
| Jun 24, 2024 16:01 | CPPclang++10.0.0 | Error | |||
| Jun 24, 2024 15:28 | CPPclang++10.0.0 | Success | 140,423+2.51 RP | ||
| Jun 24, 2024 03:56 | CPPg++9.4.0 | Success | 145,559+28.47 RP | ||
| Jun 24, 2024 03:55 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:21 | CPPclang++10.0.0 | Success | 342,396 | ||
| Jun 20, 2024 23:21 | CPPg++9.4.0 | Success | 1,028,495 | ||
| Jun 20, 2024 23:19 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:18 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:17 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:16 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:16 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 23:12 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 22:56 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 22:55 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 22:54 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 22:54 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 21:55 | CPPg++9.4.0 | Success | 329,106 | ||
| Jun 20, 2024 21:55 | CPPg++9.4.0 | Success | 326,503 | ||
| Jun 20, 2024 21:53 | CPPg++9.4.0 | Success | 333,127 | ||
| Jun 20, 2024 21:21 | CPPg++9.4.0 | Success | 321,748 | ||
| Jun 20, 2024 21:20 | CPPclang++10.0.0 | Error | |||
| Jun 20, 2024 20:26 | CPPg++9.4.0 | Success | 317,054 | ||
| Jun 20, 2024 19:42 | CPPg++9.4.0 | Success | 315,814 | ||
| Jun 20, 2024 19:41 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 18:49 | CPPg++9.4.0 | Success | 295,418 | ||
| Jun 20, 2024 18:28 | CPPg++9.4.0 | Success | 286,274 | ||
| Jun 20, 2024 18:28 | CPPg++9.4.0 | Success | 248,555+4.88 RP | ||
| Jun 20, 2024 18:27 | CPPclang++10.0.0 | Success | 282,851+0.10 RP | ||
| Jun 20, 2024 18:06 | CPPg++9.4.0 | Success | 293,059 | ||
| Jun 20, 2024 18:05 | CPPg++9.4.0 | Error | |||
| Jun 20, 2024 17:51 | CPPg++9.4.0 | Success | 283,681+29.43 RP | ||
| Jun 18, 2024 19:12 | CPPg++9.4.0 | Success | 42,289,504 | ||
| Jun 18, 2024 18:49 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 18:43 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 17:57 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 17:55 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 17:48 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 17:46 | CPPclang++10.0.0 | Error | |||
| Jun 18, 2024 17:45 | CPPclang++10.0.0 | Error | |||
| Jun 18, 2024 17:18 | CPPg++9.4.0 | Success | 2,244,356+4.46 RP | ||
| Jun 18, 2024 16:54 | CPPclang++10.0.0 | Success | 1,718,849+0.01 RP | ||
| Jun 18, 2024 16:53 | CPPg++9.4.0 | Success | 1,722,466+1.37 RP | ||
| Jun 18, 2024 16:41 | CPPg++9.4.0 | Error | |||
| Jun 18, 2024 16:17 | CPPg++9.4.0 | Success | 2,252,565+4.44 RP | ||
| Jun 11, 2024 15:56 | CPPclang++10.0.0 | Error | |||
| Jun 11, 2024 15:53 | CPPclang++10.0.0 | Error | |||
| Jun 11, 2024 15:49 | CPPclang++10.0.0 | Error | |||
| May 25, 2024 06:01 | CPPg++9.4.0 | Error | |||
| May 25, 2024 05:59 | CPPg++9.4.0 | Success | 48,806 | ||
| May 16, 2024 19:39 | GOgo1.22.3 | Success | 1,369,477+2.85 RP | ||
| May 16, 2024 19:39 | CPPg++9.4.0 | Success | 2,245,894+4.45 RP | ||
| May 11, 2024 17:35 | RUSTrust-1.78.0 | Success | 214,086 | ||
| May 11, 2024 16:48 | RUSTrust-1.78.0 | Success | 110,359 |