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 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
Apr 19, 2025 19:49Aniruddha DebAniruddha DebCPPg++13.2.0Success19,198
Apr 19, 2025 19:44Aniruddha DebAniruddha DebCPPg++13.2.0Success1,806,417
Apr 19, 2025 19:35Aniruddha DebAniruddha DebCPPg++13.2.0Error
Apr 19, 2025 19:34Aniruddha DebAniruddha DebCPPg++13.2.0Error
Apr 18, 2025 21:27Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Error
Apr 18, 2025 21:25Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success74,679
Apr 18, 2025 21:24Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success80,967
Apr 18, 2025 21:24Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success81,457
Apr 18, 2025 21:22Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success80,310
Apr 18, 2025 21:21Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Error
Apr 18, 2025 21:18Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success78,850
Apr 18, 2025 21:16Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success82,988
Apr 18, 2025 21:15Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success80,828
Apr 18, 2025 21:15Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success85,295
Apr 18, 2025 21:09Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success86,781
Apr 18, 2025 21:06Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Error
Apr 18, 2025 21:05Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Error
Apr 18, 2025 21:00Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Error
Apr 18, 2025 20:52Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success87,352
Apr 18, 2025 20:49Yuriy LyfenkoYuriy LyfenkoGOgo1.24.2Success84,426
Apr 18, 2025 20:32Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success59,116
Apr 18, 2025 20:15KeefeKeefeCPPg++13.2.0Success110,595+8.23 RP
Apr 18, 2025 20:13KeefeKeefeCPPg++13.2.0Error
Apr 18, 2025 20:09KeefeKeefeCPPg++13.2.0Error
Apr 18, 2025 20:07KeefeKeefeCPPg++13.2.0Error
Apr 18, 2025 20:04Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success60,876
Apr 18, 2025 19:54Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success62,555
Apr 18, 2025 19:42Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success64,131
Apr 18, 2025 19:35KeefeKeefeCPPg++13.2.0Success122,260
Apr 18, 2025 19:34KeefeKeefeCPPg++13.2.0Error
Apr 18, 2025 19:33Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success69,464
Apr 18, 2025 19:30Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success64,628
Apr 18, 2025 19:27Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success65,803
Apr 18, 2025 19:26KeefeKeefeCPPg++13.2.0Success121,676+42.61 RP
Apr 18, 2025 19:08Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success64,290
Apr 18, 2025 19:07Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success69,410
Apr 18, 2025 18:58Yuriy LyfenkoYuriy LyfenkoCSHARP9.0.4Success76,983
Apr 15, 2025 16:14Carl BergmanCarl BergmanRUSTrust-1.86.0Success479,659+1.43 RP
Apr 15, 2025 16:09Carl BergmanCarl BergmanRUSTrust-1.86.0Success514,990+2.40 RP
Apr 15, 2025 03:24yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:57yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:40yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:38yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:36yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:20yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:18yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 02:11yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 01:32yenw0dyenw0dRUSTrust-1.85.1Success68,445+19.70 RP
Apr 15, 2025 00:42yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 00:38yenw0dyenw0dRUSTrust-1.85.1Error
Apr 15, 2025 00:29yenw0dyenw0dRUSTrust-1.85.1Error
Apr 14, 2025 23:57yenw0dyenw0dRUSTrust-1.85.1Error
Apr 14, 2025 23:55yenw0dyenw0dRUSTrust-1.85.1Error
Apr 14, 2025 23:55yenw0dyenw0dRUSTrust-1.85.1Error
Apr 14, 2025 23:51yenw0dyenw0dRUSTrust-1.85.1Error
Apr 13, 2025 18:11yenw0dyenw0dRUSTrust-1.85.1Error
Apr 13, 2025 16:55yenw0dyenw0dRUSTrust-1.85.1Error