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
Dec 22, 2025 04:13Josu San MartinJosu San MartinCPPg++13.3.0Success56,234
Dec 22, 2025 04:12Josu San MartinJosu San MartinCPPg++13.3.0Success57,340
Dec 22, 2025 04:11Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 04:11Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 04:11Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 04:09Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 04:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 04:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 04:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 04:04Josu San MartinJosu San MartinCPPg++13.3.0Success111,021
Dec 22, 2025 04:04Josu San MartinJosu San MartinCPPg++13.3.0Success99,931
Dec 22, 2025 04:04Josu San MartinJosu San MartinCPPg++13.3.0Success312,055
Dec 22, 2025 04:03Josu San MartinJosu San MartinCPPg++13.3.0Success90,010
Dec 22, 2025 04:03Josu San MartinJosu San MartinCPPg++13.3.0Success90,283
Dec 22, 2025 04:03RussellStrongRussellStrongRUSTrust-1.92.0Success2,469,883+4.05 RP
Dec 22, 2025 03:06Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 03:06Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 03:05Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 02:51Josu San MartinJosu San MartinRUSTrust-1.92.0Success51,914
Dec 22, 2025 02:39Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 02:30Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:56Josu San MartinJosu San MartinRUSTrust-1.92.0Success52,440
Dec 22, 2025 01:54Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:54Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:54Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:51Josu San MartinJosu San MartinRUSTrust-1.92.0Success37,838
Dec 22, 2025 01:50Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 01:47Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:46Josu San MartinJosu San MartinCPPg++13.3.0Error+0.09 RP
Dec 22, 2025 01:46Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:45Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 01:41Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 01:40Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 01:40Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:40Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:40Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 22, 2025 01:38Josu San MartinJosu San MartinRUSTrust-1.92.0Success47,519
Dec 22, 2025 01:37Josu San MartinJosu San MartinRUSTrust-1.92.0Success49,216
Dec 22, 2025 01:37Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 22, 2025 00:31HattonuriHattonuriCPPg++13.3.0Error+0.99 RP
Dec 22, 2025 00:27HattonuriHattonuriCPPg++13.3.0Error
Dec 22, 2025 00:10HattonuriHattonuriCPPg++13.3.0Error
Dec 21, 2025 23:53HattonuriHattonuriCPPg++13.3.0Error
Dec 21, 2025 23:49HattonuriHattonuriCPPg++13.3.0Error
Dec 21, 2025 23:08HattonuriHattonuriCPPg++13.3.0Error+11.25 RP
Dec 21, 2025 21:22Aleksandr KrotovAleksandr KrotovCPPg++13.3.0Success1,285,360+0.06 RP
Dec 21, 2025 21:22Aleksandr KrotovAleksandr KrotovCPPg++13.3.0Success1,295,431+7.72 RP
Dec 21, 2025 20:34Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 20:31Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 20:28Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 20:26Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 20:24Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 20:18Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 19:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:07Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:07Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:07Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:04Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:04Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:00Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 19:00Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:50Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:42Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:41Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:14Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:13Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 18:13Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 21, 2025 16:12Josu San MartinJosu San MartinCPPg++13.3.0Success45,890
Dec 21, 2025 15:53Josu San MartinJosu San MartinCPPg++13.3.0Success61,991
Dec 21, 2025 12:56Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:31Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:23Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:20Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:17Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:15Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:11Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:06Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 12:03Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 11:40Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 11:37parastromparastromRUSTrust-1.90.0Success73,926+16.91 RP
Dec 21, 2025 11:33Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 11:32idatsyidatsyRUSTrust-1.92.0Success229,019
Dec 21, 2025 11:26Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success94,919
Dec 21, 2025 11:10Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success99,767
Dec 21, 2025 11:09Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success99,812
Dec 21, 2025 11:03Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:58Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:56Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success100,057
Dec 21, 2025 10:54Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success99,445
Dec 21, 2025 10:48Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:46Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:44Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:41Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:39Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:30Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success102,562
Dec 21, 2025 10:24Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 10:18Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Error
Dec 21, 2025 09:48NaseefNaseefCPPg++13.3.0Error
Dec 21, 2025 07:39KeefeKeefeCPPclang++18.1.3Success88,512
Dec 21, 2025 07:39KeefeKeefeCPPg++13.3.0Success90,879