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
May 25, 2025 14:09E SequeiraE SequeiraCPPclang++18.1.3Success99,516
May 25, 2025 14:07E SequeiraE SequeiraCPPclang++18.1.3Success99,800
May 25, 2025 14:06E SequeiraE SequeiraCPPclang++18.1.3Error
May 25, 2025 14:05E SequeiraE SequeiraCPPclang++18.1.3Success75,674
May 25, 2025 14:03E SequeiraE SequeiraGOgo1.24.3Error
May 25, 2025 14:02E SequeiraE SequeiraGOgo1.24.3Error
May 25, 2025 14:02E SequeiraE SequeiraGOgo1.24.3Error
May 25, 2025 14:02E SequeiraE SequeiraGOgo1.24.3Error
May 25, 2025 13:59E SequeiraE SequeiraGOgo1.23.3Error
May 24, 2025 23:05Daniel SavuDaniel SavuRUSTrust-1.87.0Success267,579
May 24, 2025 21:54Daniel SavuDaniel SavuRUSTrust-1.87.0Success286,274
May 24, 2025 21:53Daniel SavuDaniel SavuRUSTrust-1.87.0Error
May 24, 2025 19:12Daniel SavuDaniel SavuRUSTrust-1.87.0Error
May 24, 2025 00:09Daniel SavuDaniel SavuRUSTrust-1.87.0Success262,716+38.06 RP
May 23, 2025 23:23Daniel SavuDaniel SavuRUSTrust-1.87.0Error
May 23, 2025 17:49KeefeKeefeCPPg++13.3.0Success82,933+4.78 RP
May 23, 2025 17:47KeefeKeefeCPPg++13.3.0Success86,357+21.07 RP
May 14, 2025 02:07Carl BergmanCarl BergmanRUSTrust-1.86.0Success437,610
May 13, 2025 16:47Carl BergmanCarl BergmanRUSTrust-1.86.0Success329,171
May 13, 2025 16:12Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Success684,324+2.44 RP
May 13, 2025 16:11Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Error
May 13, 2025 15:25Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Success821,698+0.50 RP
May 13, 2025 15:14Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Success985,850
May 13, 2025 15:05Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Success856,902+3.71 RP
May 13, 2025 15:02Emanuele BolognesiEmanuele BolognesiRUSTrust-1.86.0Error
May 12, 2025 02:43Carl BergmanCarl BergmanRUSTrust-1.86.0Error
May 12, 2025 02:35Carl BergmanCarl BergmanRUSTrust-1.86.0Error
May 12, 2025 02:32Carl BergmanCarl BergmanRUSTrust-1.86.0Error
May 12, 2025 02:31Carl BergmanCarl BergmanRUSTrust-1.86.0Error
May 12, 2025 02:25Carl BergmanCarl BergmanRUSTrust-1.86.0Error
May 10, 2025 14:55Tejas G.Tejas G.CPPclang++18.1.3Error
May 10, 2025 14:53Tejas G.Tejas G.CPPclang++18.1.3Error
May 10, 2025 14:53Tejas G.Tejas G.CPPclang++18.1.3Error
May 10, 2025 14:52Tejas G.Tejas G.CPPclang++18.1.3Error
May 10, 2025 14:51Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 16:44Amit RahmanAmit RahmanCPPg++13.3.0Success1,299,402+7.70 RP
May 9, 2025 13:14Tejas G.Tejas G.CPPclang++18.1.3Success129,366
May 9, 2025 13:13Tejas G.Tejas G.CPPg++13.3.0Success138,719
May 9, 2025 13:13Tejas G.Tejas G.CPPclang++18.1.3Success129,267
May 9, 2025 13:10Tejas G.Tejas G.CPPclang++18.1.3Success129,103
May 9, 2025 13:09Tejas G.Tejas G.CPPclang++18.1.3Success129,472
May 9, 2025 11:58Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:58Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:57Tejas G.Tejas G.CPPclang++18.1.3Success160,521
May 9, 2025 11:55Tejas G.Tejas G.CPPclang++18.1.3Success162,171
May 9, 2025 11:55Tejas G.Tejas G.CPPclang++18.1.3Success162,379
May 9, 2025 11:55Tejas G.Tejas G.CPPclang++18.1.3Success159,097
May 9, 2025 11:54Tejas G.Tejas G.CPPclang++18.1.3Success155,453
May 9, 2025 11:54Tejas G.Tejas G.CPPclang++18.1.3Success160,109
May 9, 2025 11:53Tejas G.Tejas G.CPPclang++18.1.3Success153,884
May 9, 2025 11:53Tejas G.Tejas G.CPPclang++18.1.3Success160,091
May 9, 2025 11:52Tejas G.Tejas G.CPPclang++18.1.3Success160,471
May 9, 2025 11:52Tejas G.Tejas G.CPPg++13.3.0Success168,097
May 9, 2025 11:51Tejas G.Tejas G.CPPclang++18.1.3Success159,136
May 9, 2025 11:48Tejas G.Tejas G.CPPclang++18.1.3Success129,321
May 9, 2025 11:26Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:26Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:26Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:25Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:25Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:24Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:23Tejas G.Tejas G.CPPclang++18.1.3Error
May 9, 2025 11:09Tejas G.Tejas G.CPPclang++18.1.3Success679,984
May 6, 2025 15:21NoSIMD_C#NoSIMD_C#CSHARP9.0.4Success1,005,291
May 6, 2025 15:17NoSIMD_C#NoSIMD_C#CSHARP9.0.4Success1,005,286
May 6, 2025 08:00Kenneth MaplesKenneth MaplesCPPg++13.2.0Success584,048
May 6, 2025 08:00Kenneth MaplesKenneth MaplesCPPg++13.2.0Success463,695
May 6, 2025 07:59Kenneth MaplesKenneth MaplesCPPg++13.2.0Success493,874
May 6, 2025 07:56Kenneth MaplesKenneth MaplesCPPg++13.2.0Success566,800
May 6, 2025 07:50Kenneth MaplesKenneth MaplesCPPg++13.2.0Success607,957
May 6, 2025 07:49Kenneth MaplesKenneth MaplesCPPg++13.2.0Success579,472
May 6, 2025 07:47Kenneth MaplesKenneth MaplesCPPg++13.2.0Success502,403
May 2, 2025 07:58KeefeKeefeGOgo1.24.2Error
May 2, 2025 07:49KeefeKeefeGOgo1.24.2Error
Apr 30, 2025 13:47etherealetherealGOgo1.19.3Success113,619+0.52 RP
Apr 30, 2025 13:43etherealetherealGOgo1.19.3Success114,300+5.67 RP
Apr 30, 2025 13:28etherealetherealGOgo1.24.2Success122,222+2.99 RP
Apr 30, 2025 13:20etherealetherealGOgo1.24.2Success126,852+72.98 RP
Apr 30, 2025 13:18etherealetherealGOgo1.24.2Error
Apr 28, 2025 22:25Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success46,802
Apr 28, 2025 22:20Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Success44,750
Apr 28, 2025 22:20Yuriy LyfenkoYuriy LyfenkoCPPg++13.3.0Success55,043
Apr 24, 2025 19:18Carl BergmanCarl BergmanRUSTrust-1.86.0Success290,552+8.21 RP
Apr 23, 2025 09:08NoSIMD_C#NoSIMD_C#CSHARP9.0.4Success1,052,143
Apr 23, 2025 09:06NoSIMD_C#NoSIMD_C#CSHARP9.0.4Success1,060,876
Apr 22, 2025 18:32Ivo Crnkovic-RubsamenIvo Crnkovic-RubsamenRUSTrust-1.85.1Success177,086+56.47 RP
Apr 22, 2025 02:14Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 21, 2025 18:10Carl BergmanCarl BergmanRUSTrust-1.86.0Success381,610+4.10 RP
Apr 21, 2025 17:24Carl BergmanCarl BergmanRUSTrust-1.86.0Error
Apr 21, 2025 17:08Carl BergmanCarl BergmanRUSTrust-1.86.0Success452,331+1.26 RP
Apr 20, 2025 00:12Kenneth MaplesKenneth MaplesCPPg++13.2.0Error
Apr 20, 2025 00:09Kenneth MaplesKenneth MaplesCPPg++13.2.0Error
Apr 20, 2025 00:08Kenneth MaplesKenneth MaplesCPPg++13.2.0Error
Apr 20, 2025 00:07Kenneth MaplesKenneth MaplesCPPclang++18.1.3Error
Apr 20, 2025 00:06Kenneth MaplesKenneth MaplesCPPclang++18.1.3Success1,242,750
Apr 19, 2025 20:54KeefeKeefeCPPg++13.2.0Error
Apr 19, 2025 20:52KeefeKeefeCPPg++13.2.0Success105,567+1.81 RP
Apr 19, 2025 20:49KeefeKeefeCPPg++13.2.0Success107,629+2.09 RP
Apr 19, 2025 20:46KeefeKeefeCPPg++13.2.0Error
Apr 19, 2025 20:43KeefeKeefeCPPg++13.2.0Success110,100+0.41 RP