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 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
Mar 30, 2025 23:11Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success133,945
Mar 30, 2025 23:10Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 30, 2025 23:10Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 30, 2025 23:06Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 30, 2025 22:58Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 30, 2025 22:57Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Mar 30, 2025 22:57Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 22:55Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 21:38Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 08:14Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 08:11Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 08:10Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success145,160
Mar 30, 2025 08:07Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 07:42Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success157,869
Mar 30, 2025 07:42Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success144,666
Mar 30, 2025 03:07Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success134,262
Mar 30, 2025 03:06Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success134,857
Mar 30, 2025 02:34Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Mar 30, 2025 01:31Code HintsCode HintsRUSTrust-1.85.1Success110,740
Mar 30, 2025 01:08Code HintsCode HintsRUSTrust-1.85.1Success111,198
Mar 30, 2025 01:02Code HintsCode HintsRUSTrust-1.85.1Error
Mar 30, 2025 01:00Code HintsCode HintsRUSTrust-1.85.1Success110,864
Mar 30, 2025 00:59Code HintsCode HintsRUSTrust-1.85.1Error
Mar 30, 2025 00:55Code HintsCode HintsRUSTrust-1.85.1Success112,957
Mar 30, 2025 00:53Code HintsCode HintsRUSTrust-1.85.1Success111,978
Mar 30, 2025 00:50Code HintsCode HintsRUSTrust-1.85.1Success110,250
Mar 30, 2025 00:50Code HintsCode HintsRUSTrust-1.85.1Success114,460
Mar 30, 2025 00:43Code HintsCode HintsRUSTrust-1.85.1Success108,679
Mar 30, 2025 00:42Code HintsCode HintsRUSTrust-1.85.1Success109,548
Mar 30, 2025 00:00undenuicapundenuicapRUSTrust-1.84.1Success110,229
Mar 29, 2025 23:52Code HintsCode HintsRUSTrust-1.85.1Success109,459
Mar 29, 2025 23:51Code HintsCode HintsRUSTrust-1.85.1Success109,333
Mar 29, 2025 23:46Code HintsCode HintsRUSTrust-1.85.1Success109,645
Mar 29, 2025 23:29Code HintsCode HintsRUSTrust-1.85.1Success109,159
Mar 29, 2025 23:29Code HintsCode HintsRUSTrust-1.85.1Error
Mar 29, 2025 23:28Code HintsCode HintsRUSTrust-1.85.1Error
Mar 29, 2025 23:28Code HintsCode HintsRUSTrust-1.85.1Error
Mar 29, 2025 23:27Code HintsCode HintsRUSTrust-1.85.1Error
Mar 29, 2025 23:26Code HintsCode HintsRUSTrust-1.85.1Success113,850
Mar 29, 2025 23:25Code HintsCode HintsRUSTrust-1.85.1Success109,828
Mar 29, 2025 23:22Code HintsCode HintsRUSTrust-1.85.1Success110,464
Mar 29, 2025 23:22Code HintsCode HintsRUSTrust-1.85.1Success110,688
Mar 29, 2025 23:19Code HintsCode HintsRUSTrust-1.85.1Success109,343