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