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 19, 2025 00:51CadovvlCadovvlCPPg++13.3.0Success124,700
Aug 19, 2025 00:50CadovvlCadovvlCPPg++13.3.0Success72,574
Aug 19, 2025 00:50CadovvlCadovvlCPPg++13.3.0Success64,195+4.27 RP
Aug 19, 2025 00:49CadovvlCadovvlCPPg++13.3.0Success105,391
Aug 19, 2025 00:49CadovvlCadovvlCPPg++13.3.0Success94,919
Aug 19, 2025 00:47CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 00:47Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success77,024
Aug 19, 2025 00:38CadovvlCadovvlCPPg++13.3.0Success66,300
Aug 19, 2025 00:29CadovvlCadovvlCPPg++13.3.0Success66,471
Aug 19, 2025 00:27CadovvlCadovvlCPPg++13.3.0Success66,093
Aug 19, 2025 00:25CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 00:25CadovvlCadovvlCPPg++13.3.0Success66,003+9.00 RP
Aug 18, 2025 23:49CadovvlCadovvlCPPg++13.3.0Success108,397
Aug 18, 2025 23:49CadovvlCadovvlCPPg++13.3.0Success110,971
Aug 18, 2025 23:47CadovvlCadovvlCPPclang++18.1.3Error
Aug 18, 2025 23:42CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 23:23CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:52CadovvlCadovvlCPPSuccess74,071
Aug 18, 2025 21:30CadovvlCadovvlCPPg++13.3.0Success70,512
Aug 18, 2025 21:30CadovvlCadovvlCPPg++13.3.0Success71,345
Aug 18, 2025 21:30CadovvlCadovvlCPPg++13.3.0Success75,245
Aug 18, 2025 21:29CadovvlCadovvlCPPg++13.3.0Success73,060
Aug 18, 2025 21:27CadovvlCadovvlCPPg++13.3.0Success78,683
Aug 18, 2025 21:27CadovvlCadovvlCPPg++13.3.0Success71,212
Aug 18, 2025 21:26CadovvlCadovvlCPPg++13.3.0Success207,566
Aug 18, 2025 21:25CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:24CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:24CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:21CadovvlCadovvlCPPg++13.3.0Success70,393
Aug 18, 2025 21:19CadovvlCadovvlCPPg++13.3.0Success70,510
Aug 18, 2025 21:14CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:12CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 21:06CadovvlCadovvlCPPg++13.3.0Success149,259
Aug 18, 2025 20:59CadovvlCadovvlCPPg++13.3.0Success111,716
Aug 18, 2025 20:58CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 20:51CadovvlCadovvlCPPg++13.3.0Success70,278
Aug 18, 2025 20:50CadovvlCadovvlCPPg++13.3.0Success70,591
Aug 18, 2025 20:50CadovvlCadovvlCPPg++13.3.0Success70,719
Aug 18, 2025 20:46CadovvlCadovvlCPPg++13.3.0Success70,236
Aug 18, 2025 20:45CadovvlCadovvlCPPg++13.3.0Success70,650
Aug 18, 2025 20:45CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 20:43CadovvlCadovvlCPPg++13.3.0Error
Aug 18, 2025 20:41CadovvlCadovvlCPPg++13.3.0Error
Aug 16, 2025 20:50MylesMylesCPPg++13.3.0Success122,881+12.87 RP
Aug 16, 2025 20:38MylesMylesCPPg++13.3.0Success145,967+68.51 RP
Aug 16, 2025 16:58MylesMylesCPPg++13.3.0Error
Aug 16, 2025 16:57MylesMylesCPPg++13.3.0Error
Aug 16, 2025 16:56MylesMylesCPPg++13.3.0Error
Aug 16, 2025 16:52MylesMylesCPPg++13.3.0Error
Aug 16, 2025 16:50MylesMylesCPPg++13.3.0Error
Aug 16, 2025 16:43MylesMylesCPPg++13.3.0Error
Aug 16, 2025 15:51MylesMylesCPPg++13.3.0Error
Aug 16, 2025 15:50MylesMylesCPPg++13.3.0Error
Aug 16, 2025 15:49MylesMylesCPPg++13.3.0Error
Aug 16, 2025 15:32MylesMylesCPPclang++18.1.3Error
Aug 16, 2025 15:31MylesMylesCPPg++13.3.0Error
Aug 16, 2025 15:30MylesMylesCPPg++13.3.0Error
Aug 13, 2025 13:01CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:46CadovvlCadovvlCPPg++13.3.0Success71,440
Aug 13, 2025 12:45CadovvlCadovvlCPPg++13.3.0Success72,240
Aug 13, 2025 12:43CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:43CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:41CadovvlCadovvlCPPg++13.3.0Success70,522
Aug 13, 2025 12:39CadovvlCadovvlCPPg++13.3.0Success70,614
Aug 13, 2025 12:39CadovvlCadovvlCPPg++13.3.0Success70,453
Aug 13, 2025 12:39CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:38CadovvlCadovvlCPPg++13.3.0Success70,652
Aug 13, 2025 12:38CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:37CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:37CadovvlCadovvlCPPg++13.3.0Success71,924
Aug 13, 2025 12:37CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:36CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:36CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:35CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:34CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:33CadovvlCadovvlCPPg++13.3.0Success70,171+0.67 RP
Aug 13, 2025 12:33CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:32CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:32CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 12:32CadovvlCadovvlCPPg++13.3.0Success70,753
Aug 13, 2025 12:31CadovvlCadovvlCPPg++13.3.0Success71,522
Aug 13, 2025 12:31CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 09:11CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 09:09CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 09:09CadovvlCadovvlCPPg++13.3.0Success71,333
Aug 13, 2025 09:08CadovvlCadovvlCPPg++13.3.0Success71,779
Aug 13, 2025 09:08CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 09:05CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 09:01CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 08:52CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 08:50CadovvlCadovvlCPPg++13.3.0Success70,690
Aug 13, 2025 08:49CadovvlCadovvlCPPg++13.3.0Success70,784
Aug 13, 2025 08:48CadovvlCadovvlCPPg++13.3.0Success71,172
Aug 13, 2025 08:48CadovvlCadovvlCPPg++13.3.0Success70,990
Aug 13, 2025 08:47CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 08:46CadovvlCadovvlCPPg++13.3.0Error
Aug 13, 2025 08:45CadovvlCadovvlCPPg++13.3.0Error
Aug 12, 2025 10:47CadovvlCadovvlCPPg++13.3.0Error
Aug 11, 2025 20:59CadovvlCadovvlCPPg++13.3.0Success70,514
Aug 11, 2025 20:54CadovvlCadovvlCPPg++13.3.0Success70,548