Order book Yuriy Lyfenko

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 AuthorLanguageStatus Score
Apr 13, 2025 16:52yenw0dyenw0dRUSTrust-1.85.1Error
Apr 12, 2025 02:15Carl BergmanCarl BergmanRUSTrust-1.86.0Success643,960
Apr 11, 2025 20:02Carl BergmanCarl BergmanRUSTrust-1.86.0Success998,764
Apr 11, 2025 19:28Carl BergmanCarl BergmanRUSTrust-1.86.0Success594,272
Apr 11, 2025 18:02Carl BergmanCarl BergmanRUSTrust-1.86.0Success596,348
Apr 11, 2025 18:01Carl BergmanCarl BergmanRUSTrust-1.86.0Success603,191
Apr 11, 2025 18:01Carl BergmanCarl BergmanRUSTrust-1.86.0Success587,695+0.05 RP
Apr 11, 2025 17:37Carl BergmanCarl BergmanRUSTrust-1.86.0Success641,133
Apr 11, 2025 17:34Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 17:14Carl BergmanCarl BergmanRUSTrust-1.86.0Success602,584
Apr 11, 2025 17:01Carl BergmanCarl BergmanRUSTrust-1.86.0Success589,553+8.94 RP
Apr 11, 2025 15:43Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:54Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:48Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:40Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:40Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:09Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 14:07Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 13:59Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 13:44Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 13:42Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 13:41Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 13:33Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:34Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:30Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:22Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:22Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:14Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:12Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 05:05Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 11, 2025 04:02Carl BergmanCarl BergmanCPPclang++18.1.3Success1,246,071+8.03 RP
Apr 11, 2025 01:51yenw0dyenw0dRUSTrust-1.85.1Success79,114+45.51 RP
Apr 10, 2025 17:34yenw0dyenw0dRUSTrust-1.85.1Success269,817
Apr 10, 2025 17:33yenw0dyenw0dRUSTrust-1.85.1Success345,326
Apr 10, 2025 17:23yenw0dyenw0dRUSTrust-1.85.1Success272,938
Apr 10, 2025 17:21yenw0dyenw0dRUSTrust-1.85.1Error
Apr 10, 2025 17:18yenw0dyenw0dRUSTrust-1.85.1Success123,629+5.26 RP
Apr 10, 2025 17:15yenw0dyenw0dRUSTrust-1.85.1Success278,971
Apr 8, 2025 07:31NoSIMD_C#NoSIMD_C#CPPclang++18.1.3Success1,262,672
Apr 8, 2025 07:20NoSIMD_C#NoSIMD_C#CPPg++13.2.0Success1,485,516
Apr 7, 2025 16:03Bernard TeoBernard TeoCPPg++13.2.0Success1,250,436
Apr 7, 2025 16:01Bernard TeoBernard TeoCPPg++13.2.0Success1,243,905
Apr 7, 2025 16:00Bernard TeoBernard TeoCPPg++13.2.0Success1,239,629
Apr 7, 2025 16:00Bernard TeoBernard TeoCPPg++13.2.0Error
Apr 7, 2025 15:58Bernard TeoBernard TeoCPPg++13.2.0Success1,239,183+8.07 RP
Apr 2, 2025 14:51Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Apr 2, 2025 13:54Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success52,636
Apr 2, 2025 13:50Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success97,198
Apr 2, 2025 10:35NoSIMD_C#NoSIMD_C#CSHARP9.0.3Success1,059,795
Apr 2, 2025 10:25NoSIMD_C#NoSIMD_C#CSHARP9.0.3Success1,057,234
Apr 2, 2025 04:27Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success45,571
Apr 2, 2025 04:22Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Apr 2, 2025 04:19Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success46,366
Apr 2, 2025 04:18Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Apr 2, 2025 04:17Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success46,388
Apr 1, 2025 15:15Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success47,803
Apr 1, 2025 14:49Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success48,116
Apr 1, 2025 14:47Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success47,016
Apr 1, 2025 13:33Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success107,550
Apr 1, 2025 13:28Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success58,093
Apr 1, 2025 13:26Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Apr 1, 2025 13:24Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Apr 1, 2025 09:37NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 09:07NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 09:01NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 08:58NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 08:41NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 07:30NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 07:18NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 07:16NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 07:08NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 07:03NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 06:47NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 06:06NoSIMD_C#NoSIMD_C#CSHARP9.0.3Success1,009,890
Apr 1, 2025 05:59NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 05:56NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Apr 1, 2025 05:48NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 23:03Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success59,662
Mar 31, 2025 21:50Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success60,641
Mar 31, 2025 20:52Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success59,369
Mar 31, 2025 20:37Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 31, 2025 20:33Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 31, 2025 20:04Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success57,260
Mar 31, 2025 19:51Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success60,169
Mar 31, 2025 19:46Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success66,628
Mar 31, 2025 19:45Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success70,000
Mar 31, 2025 19:42Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success65,612
Mar 31, 2025 19:42Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success68,416
Mar 31, 2025 19:36Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success64,048
Mar 31, 2025 17:49Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success61,878
Mar 31, 2025 17:48Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success72,222
Mar 31, 2025 17:42Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success62,597
Mar 31, 2025 14:28NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 14:26NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 14:24NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 14:19NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 14:14NoSIMD_C#NoSIMD_C#CSHARP9.0.3Error
Mar 31, 2025 13:40NoSIMD_C#NoSIMD_C#CSHARP9.0.3Success1,006,302
Mar 30, 2025 23:35Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success643,898
Mar 30, 2025 23:34Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success645,267