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 08:50CadovvlCadovvlCPPg++13.3.0Success60,836+0.42 RP
Aug 19, 2025 08:49CadovvlCadovvlCPPg++13.3.0Success62,188
Aug 19, 2025 08:46CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 07:35CadovvlCadovvlCPPg++13.3.0Success61,028
Aug 19, 2025 07:33CadovvlCadovvlCPPg++13.3.0Success61,909
Aug 19, 2025 07:33CadovvlCadovvlCPPg++13.3.0Success69,757
Aug 19, 2025 07:32CadovvlCadovvlCPPg++13.3.0Success61,000
Aug 19, 2025 07:31CadovvlCadovvlCPPg++13.3.0Success66,172
Aug 19, 2025 07:31CadovvlCadovvlCPPg++13.3.0Success64,836
Aug 19, 2025 07:30CadovvlCadovvlCPPg++13.3.0Success65,822
Aug 19, 2025 07:22CadovvlCadovvlCPPg++13.3.0Success60,990+2.50 RP
Aug 19, 2025 07:20CadovvlCadovvlCPPg++13.3.0Success66,633
Aug 19, 2025 07:16CadovvlCadovvlCPPg++13.3.0Success68,319
Aug 19, 2025 07:12CadovvlCadovvlCPPg++13.3.0Success61,936+0.65 RP
Aug 19, 2025 07:09CadovvlCadovvlCPPg++13.3.0Success62,188+0.48 RP
Aug 19, 2025 07:08CadovvlCadovvlCPPg++13.3.0Success81,209
Aug 19, 2025 07:07CadovvlCadovvlCPPg++13.3.0Success68,766
Aug 19, 2025 02:20CadovvlCadovvlCPPg++13.3.0Success63,112
Aug 19, 2025 02:18CadovvlCadovvlCPPg++13.3.0Success64,452
Aug 19, 2025 02:17CadovvlCadovvlCPPg++13.3.0Success62,374+2.95 RP
Aug 19, 2025 02:15CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 01:38CadovvlCadovvlCPPg++13.3.0Success64,076
Aug 19, 2025 01:36CadovvlCadovvlCPPg++13.3.0Success63,948
Aug 19, 2025 01:28CadovvlCadovvlCPPg++13.3.0Success63,543+0.35 RP
Aug 19, 2025 01:27CadovvlCadovvlCPPg++13.3.0Success65,533
Aug 19, 2025 01:27CadovvlCadovvlCPPg++13.3.0Success100,084
Aug 19, 2025 01:17CadovvlCadovvlCPPg++13.3.0Success64,752
Aug 19, 2025 01:16CadovvlCadovvlCPPg++13.3.0Success71,916
Aug 19, 2025 01:00CadovvlCadovvlCPPg++13.3.0Success65,574
Aug 19, 2025 00:58CadovvlCadovvlCPPg++13.3.0Success63,684+1.25 RP
Aug 19, 2025 00:57CadovvlCadovvlCPPg++13.3.0Success66,328
Aug 19, 2025 00:53CadovvlCadovvlCPPg++13.3.0Success64,426
Aug 19, 2025 00:52CadovvlCadovvlCPPg++13.3.0Success103,522
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