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
Aug 5, 2025 12:41CadovvlCadovvlCPPg++13.3.0Success74,843
Aug 5, 2025 12:40CadovvlCadovvlCPPg++13.3.0Success74,390
Aug 5, 2025 12:29CadovvlCadovvlCPPg++13.3.0Success73,041
Aug 5, 2025 12:19CadovvlCadovvlCPPg++13.3.0Success73,967
Aug 5, 2025 12:18CadovvlCadovvlCPPg++13.3.0Success73,447
Aug 5, 2025 12:18CadovvlCadovvlCPPclang++18.1.3Success102,779
Aug 5, 2025 12:15CadovvlCadovvlCPPclang++18.1.3Error
Aug 5, 2025 12:15CadovvlCadovvlCPPg++13.3.0Error
Aug 5, 2025 09:03CadovvlCadovvlCPPg++13.3.0Success106,938
Aug 5, 2025 05:06Drew GarciaDrew GarciaCPPclang++18.1.3Error
Aug 5, 2025 05:05Drew GarciaDrew GarciaCPPclang++18.1.3Error
Aug 5, 2025 05:02Drew GarciaDrew GarciaCPPclang++18.1.3Error
Aug 5, 2025 02:44Drew GarciaDrew GarciaCPPclang++18.1.3Success142,934
Aug 5, 2025 02:42Drew GarciaDrew GarciaCPPclang++18.1.3Error
Aug 5, 2025 02:37Drew GarciaDrew GarciaCPPclang++18.1.3Success89,498+27.79 RP
Aug 5, 2025 02:32Drew GarciaDrew GarciaCPPclang++18.1.3Success120,236
Aug 5, 2025 02:29Drew GarciaDrew GarciaCPPclang++18.1.3Success119,128+3.02 RP
Aug 5, 2025 02:10Drew GarciaDrew GarciaCPPclang++18.1.3Success123,569+29.28 RP
Aug 5, 2025 02:06Drew GarciaDrew GarciaCPPclang++18.1.3Success193,641+24.18 RP
Aug 5, 2025 02:02Drew GarciaDrew GarciaCPPclang++18.1.3Success364,076+27.47 RP
Aug 4, 2025 13:24CadovvlCadovvlCPPg++13.3.0Success72,247
Aug 4, 2025 13:22CadovvlCadovvlCPPg++13.3.0Success76,071
Aug 4, 2025 13:20CadovvlCadovvlCPPg++13.3.0Success76,183
Aug 4, 2025 13:17CadovvlCadovvlCPPg++13.3.0Success82,064
Aug 4, 2025 10:42CadovvlCadovvlCPPg++13.3.0Success75,448
Aug 3, 2025 10:54parastromparastromRUSTrust-1.87.0Success114,345
Aug 3, 2025 10:53parastromparastromRUSTrust-1.87.0Success118,038
Aug 2, 2025 14:29parastromparastromRUSTrust-1.87.0Success118,188
Aug 2, 2025 14:27parastromparastromRUSTrust-1.87.0Success125,862
Aug 2, 2025 12:18parastromparastromRUSTrust-1.87.0Success123,653
Aug 2, 2025 12:11parastromparastromRUSTrust-1.87.0Success155,440
Aug 2, 2025 12:10parastromparastromRUSTrust-1.87.0Success218,779
Aug 2, 2025 09:41parastromparastromRUSTrust-1.87.0Error
Jul 30, 2025 14:58Leonardo Pedrozo AmaralLeonardo Pedrozo AmaralCPPg++13.3.0Success966,840+2.65 RP
Jul 29, 2025 07:54CadovvlCadovvlCPPg++13.3.0Success75,809
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