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 11, 2025 20:52CadovvlCadovvlCPPg++13.3.0Success70,526
Aug 11, 2025 20:45CadovvlCadovvlCPPg++13.3.0Success70,500+0.01 RP
Aug 11, 2025 20:43CadovvlCadovvlCPPg++13.3.0Success82,084
Aug 11, 2025 20:33CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 20:27CadovvlCadovvlCPPg++13.3.0Success70,591
Aug 11, 2025 20:25CadovvlCadovvlCPPg++13.3.0Success70,605
Aug 11, 2025 20:24CadovvlCadovvlCPPg++13.3.0Success70,507+0.06 RP
Aug 11, 2025 08:14CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 08:14CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 08:11CadovvlCadovvlCPPg++13.3.0Success70,800
Aug 11, 2025 08:10CadovvlCadovvlCPPg++13.3.0Success73,947
Aug 11, 2025 08:08CadovvlCadovvlCPPg++13.3.0Success74,236
Aug 11, 2025 07:53CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 07:51CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 07:44CadovvlCadovvlCPPg++13.3.0Success70,966
Aug 11, 2025 07:43CadovvlCadovvlCPPg++13.3.0Success71,260
Aug 11, 2025 07:43CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 07:41CadovvlCadovvlCPPg++13.3.0Success70,664
Aug 11, 2025 07:41CadovvlCadovvlCPPg++13.3.0Success70,969
Aug 11, 2025 07:37CadovvlCadovvlCPPg++13.3.0Success70,779
Aug 11, 2025 07:36CadovvlCadovvlCPPg++13.3.0Success71,484
Aug 11, 2025 07:33CadovvlCadovvlCPPg++13.3.0Success79,386
Aug 11, 2025 07:24CadovvlCadovvlCPPg++13.3.0Success70,538+0.29 RP
Aug 11, 2025 07:23CadovvlCadovvlCPPg++13.3.0Success70,778
Aug 8, 2025 12:41CadovvlCadovvlCPPg++13.3.0Success85,603
Aug 8, 2025 12:39CadovvlCadovvlCPPg++13.3.0Success75,091
Aug 8, 2025 12:31CadovvlCadovvlCPPg++13.3.0Error
Aug 8, 2025 11:54CadovvlCadovvlCPPg++13.3.0Success70,684+0.44 RP
Aug 8, 2025 11:25CadovvlCadovvlCPPg++13.3.0Success86,174
Aug 8, 2025 11:24CadovvlCadovvlCPPg++13.3.0Error
Aug 8, 2025 10:57CadovvlCadovvlCPPg++13.3.0Success74,678
Aug 7, 2025 13:22CadovvlCadovvlCPPg++13.3.0Success70,903+0.24 RP
Aug 7, 2025 13:22CadovvlCadovvlCPPg++13.3.0Success104,719
Aug 7, 2025 13:21CadovvlCadovvlCPPg++13.3.0Success105,212
Aug 7, 2025 13:20CadovvlCadovvlCPPg++13.3.0Error
Aug 7, 2025 13:18CadovvlCadovvlCPPg++13.3.0Success71,026+2.34 RP
Aug 7, 2025 13:13CadovvlCadovvlCPPg++13.3.0Success72,226+0.02 RP
Aug 7, 2025 13:13CadovvlCadovvlCPPg++13.3.0Success73,205
Aug 7, 2025 13:11CadovvlCadovvlCPPg++13.3.0Success72,512
Aug 7, 2025 13:10CadovvlCadovvlCPPg++13.3.0Error
Aug 7, 2025 13:08CadovvlCadovvlCPPg++13.3.0Success72,381
Aug 7, 2025 13:08CadovvlCadovvlCPPg++13.3.0Success72,622
Aug 7, 2025 13:07CadovvlCadovvlCPPg++13.3.0Success75,059
Aug 7, 2025 13:06CadovvlCadovvlCPPg++13.3.0Success75,409
Aug 7, 2025 13:03CadovvlCadovvlCPPg++13.3.0Success73,678
Aug 7, 2025 13:02CadovvlCadovvlCPPg++13.3.0Success74,019
Aug 7, 2025 13:02CadovvlCadovvlCPPg++13.3.0Success114,416
Aug 7, 2025 12:59CadovvlCadovvlCPPg++13.3.0Success73,100
Aug 7, 2025 12:59CadovvlCadovvlCPPg++13.3.0Success74,879
Aug 7, 2025 12:58CadovvlCadovvlCPPg++13.3.0Success73,493
Aug 7, 2025 12:57CadovvlCadovvlCPPg++13.3.0Success74,841
Aug 7, 2025 12:53CadovvlCadovvlCPPg++13.3.0Success73,972
Aug 7, 2025 12:43CadovvlCadovvlCPPg++13.3.0Success73,929
Aug 7, 2025 12:40CadovvlCadovvlCPPg++13.3.0Success74,055
Aug 5, 2025 13:30CadovvlCadovvlCPPg++13.3.0Success74,103
Aug 5, 2025 13:23CadovvlCadovvlCPPg++13.3.0Success73,943
Aug 5, 2025 13:19CadovvlCadovvlCPPg++13.3.0Success74,084
Aug 5, 2025 13:17CadovvlCadovvlCPPSuccess77,176
Aug 5, 2025 13:13CadovvlCadovvlCPPg++13.3.0Success73,290
Aug 5, 2025 13:11CadovvlCadovvlCPPg++13.3.0Success79,436
Aug 5, 2025 12:49CadovvlCadovvlCPPg++13.3.0Success73,288
Aug 5, 2025 12:49CadovvlCadovvlCPPg++13.3.0Success73,079
Aug 5, 2025 12:48CadovvlCadovvlCPPg++13.3.0Error
Aug 5, 2025 12:46CadovvlCadovvlCPPg++13.3.0Success73,288
Aug 5, 2025 12:44CadovvlCadovvlCPPg++13.3.0Success73,531
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