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 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
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