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
Jul 29, 2025 07:52CadovvlCadovvlCPPg++13.3.0Success75,369
Jul 29, 2025 07:25CadovvlCadovvlCPPg++13.3.0Success72,497
Jul 29, 2025 07:19CadovvlCadovvlCPPg++13.3.0Error
Jul 29, 2025 07:17CadovvlCadovvlCPPg++13.3.0Success74,362
Jul 28, 2025 12:55Raj ChandakRaj ChandakCPPg++13.3.0Error
Jul 28, 2025 12:52Raj ChandakRaj ChandakCPPg++13.3.0Success1,271,043+7.87 RP
Jul 26, 2025 11:20CadovvlCadovvlCPPg++13.3.0Success74,322
Jul 26, 2025 11:18CadovvlCadovvlCPPg++13.3.0Success74,717
Jul 25, 2025 19:06Leonardo Pedrozo AmaralLeonardo Pedrozo AmaralCPPg++13.3.0Success1,299,993+7.69 RP
Jul 25, 2025 14:30CadovvlCadovvlCPPclang++18.1.3Success112,947
Jul 25, 2025 14:28CadovvlCadovvlCPPg++13.3.0Success86,029
Jul 25, 2025 14:12CadovvlCadovvlCPPg++13.3.0Success72,279
Jul 25, 2025 14:07CadovvlCadovvlCPPg++13.3.0Success73,200
Jul 25, 2025 14:02CadovvlCadovvlCPPclang++18.1.3Success96,926
Jul 25, 2025 14:01CadovvlCadovvlCPPg++13.3.0Success72,541
Jul 25, 2025 14:01CadovvlCadovvlCPPg++13.3.0Success72,279
Jul 25, 2025 14:01CadovvlCadovvlCPPg++13.3.0Success72,503
Jul 25, 2025 13:59CadovvlCadovvlCPPg++13.3.0Success72,397
Jul 25, 2025 13:58CadovvlCadovvlCPPclang++18.1.3Success82,567
Jul 25, 2025 13:58CadovvlCadovvlCPPclang++18.1.3Success92,955
Jul 25, 2025 13:57CadovvlCadovvlCPPg++13.3.0Success72,352
Jul 25, 2025 13:56CadovvlCadovvlCPPclang++18.1.3Success92,650
Jul 25, 2025 13:56CadovvlCadovvlCPPg++13.3.0Success72,307
Jul 25, 2025 13:49CadovvlCadovvlCPPg++13.3.0Success89,453
Jul 25, 2025 13:48CadovvlCadovvlCPPclang++18.1.3Success83,819
Jul 25, 2025 13:46CadovvlCadovvlCPPg++13.3.0Success79,440
Jul 25, 2025 13:44CadovvlCadovvlCPPg++13.3.0Success84,874
Jul 25, 2025 13:43CadovvlCadovvlCPPclang++18.1.3Success87,036
Jul 25, 2025 12:31CadovvlCadovvlCPPg++13.3.0Success72,347
Jul 25, 2025 12:29CadovvlCadovvlCPPg++13.3.0Success72,238+0.02 RP
Jul 25, 2025 12:26CadovvlCadovvlCPPg++13.3.0Success72,481
Jul 25, 2025 12:25CadovvlCadovvlCPPg++13.3.0Success83,026
Jul 25, 2025 12:24CadovvlCadovvlCPPg++13.3.0Success72,543
Jul 25, 2025 12:22CadovvlCadovvlCPPg++13.3.0Success72,457
Jul 25, 2025 12:21CadovvlCadovvlCPPg++13.3.0Success72,247+0.19 RP
Jul 25, 2025 12:20CadovvlCadovvlCPPg++13.3.0Success72,347+0.27 RP
Jul 25, 2025 12:18CadovvlCadovvlCPPg++13.3.0Success72,486+1.62 RP
Jul 21, 2025 05:33CadovvlCadovvlCPPg++13.3.0Success79,474
Jul 21, 2025 05:32CadovvlCadovvlCPPg++13.3.0Success80,012
Jul 21, 2025 05:32CadovvlCadovvlCPPg++13.3.0Error
Jul 21, 2025 05:30CadovvlCadovvlCPPg++13.3.0Success85,147
Jul 21, 2025 05:28CadovvlCadovvlCPPg++13.3.0Success79,605
Jul 21, 2025 05:28CadovvlCadovvlCPPg++13.3.0Success73,347+0.80 RP
Jul 21, 2025 05:24CadovvlCadovvlCPPg++13.3.0Success76,105
Jul 21, 2025 05:21CadovvlCadovvlCPPg++13.3.0Success79,716
Jul 21, 2025 05:19CadovvlCadovvlCPPg++13.3.0Success76,795
Jul 20, 2025 19:28CadovvlCadovvlCPPg++13.3.0Success76,105
Jul 20, 2025 17:57CadovvlCadovvlCPPg++13.3.0Success74,052
Jul 20, 2025 17:31CadovvlCadovvlCPPclang++18.1.3Success553,469
Jul 20, 2025 17:24CadovvlCadovvlCPPg++13.3.0Success180,105
Jul 18, 2025 15:16Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success76,297
Jul 18, 2025 15:12Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success75,816
Jul 18, 2025 15:12Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success75,155
Jul 18, 2025 15:07Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 18, 2025 15:02Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success75,672
Jul 18, 2025 15:02Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success75,783
Jul 18, 2025 15:01Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success77,395
Jul 18, 2025 15:00Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success77,378
Jul 18, 2025 15:00Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Error
Jul 18, 2025 14:57Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Error
Jul 18, 2025 14:54Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 18, 2025 14:14CadovvlCadovvlCPPg++13.3.0Success169,636
Jul 18, 2025 11:23CadovvlCadovvlCPPg++13.3.0Success78,667
Jul 18, 2025 11:21CadovvlCadovvlCPPg++13.3.0Success93,290
Jul 18, 2025 11:20CadovvlCadovvlCPPg++13.3.0Success93,869
Jul 18, 2025 11:18CadovvlCadovvlCPPg++13.3.0Success93,883
Jul 18, 2025 11:10CadovvlCadovvlCPPg++13.3.0Success92,943
Jul 18, 2025 11:09CadovvlCadovvlCPPg++13.3.0Success82,969
Jul 18, 2025 11:04CadovvlCadovvlCPPg++13.3.0Success98,557
Jul 18, 2025 11:03CadovvlCadovvlCPPg++13.3.0Success89,459
Jul 18, 2025 10:59CadovvlCadovvlCPPg++13.3.0Success79,591
Jul 18, 2025 10:57CadovvlCadovvlCPPg++13.3.0Success73,926
Jul 18, 2025 03:11stdspstdspRUSTrust-1.88.0Success353,829
Jul 18, 2025 02:59stdspstdspRUSTrust-1.88.0Success160,824
Jul 18, 2025 02:57stdspstdspRUSTrust-1.88.0Error
Jul 18, 2025 02:44stdspstdspRUSTrust-1.88.0Success162,531
Jul 18, 2025 02:43stdspstdspRUSTrust-1.88.0Success161,883
Jul 18, 2025 02:42stdspstdspRUSTrust-1.88.0Success162,722
Jul 18, 2025 02:41stdspstdspRUSTrust-1.88.0Success162,000
Jul 18, 2025 02:39stdspstdspRUSTrust-1.88.0Success162,674
Jul 18, 2025 02:37stdspstdspRUSTrust-1.88.0Success161,910
Jul 18, 2025 02:34stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:37stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:35stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:33stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:31stdspstdspRUSTrust-1.88.0Error
Jul 16, 2025 23:55TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 16:06TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 15:01TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 12:55TakopiTakopiRUSTrust-1.88.0Success152,795+0.19 RP
Jul 16, 2025 12:29TakopiTakopiRUSTrust-1.88.0Success153,236+36.12 RP
Jul 16, 2025 11:58TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:56TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:53TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:48TakopiTakopiRUSTrust-1.88.0Success343,183+0.74 RP
Jul 16, 2025 10:33TakopiTakopiRUSTrust-1.88.0Success352,152+28.40 RP
Jul 16, 2025 05:56CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:42CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:41CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:40CadovvlCadovvlCPPg++13.3.0Error