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
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:15Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 14:14Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 12:51Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 12:44Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 12:38Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 12:36Jem BishopJem BishopRUSTrust-1.93.1Success3,841+110.14 RP
Feb 24, 2026 12:33Jem BishopJem BishopRUSTrust-1.93.1Success3,924+249.54 RP
Feb 24, 2026 12:29Jem BishopJem BishopRUSTrust-1.93.1Success4,126+1'012.96 RP
Feb 24, 2026 12:28Jem BishopJem BishopRUSTrust-1.93.1Success5,281
Feb 24, 2026 11:54Jem BishopJem BishopRUSTrust-1.93.1Success5,216+191.36 RP
Feb 24, 2026 11:50Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 11:48Jem BishopJem BishopRUSTrust-1.93.1Error
Feb 24, 2026 11:47Jem BishopJem BishopRUSTrust-1.93.1Success5,490+792.20 RP
Feb 24, 2026 11:45Jem BishopJem BishopRUSTrust-1.93.1Success8,479+915.27 RP
Feb 24, 2026 11:15Jem BishopJem BishopRUSTrust-1.93.1Success37,862+21.07 RP
Feb 23, 2026 18:07Jem BishopJem BishopRUSTrust-1.93.1Success41,579
Feb 23, 2026 18:02Jem BishopJem BishopRUSTrust-1.93.1Success42,029
Feb 23, 2026 18:00Aatif SyedAatif SyedRUSTrust-1.93.1Error
Feb 23, 2026 17:16Aatif SyedAatif SyedRUSTrust-1.93.1Success75,672+36.91 RP
Feb 23, 2026 17:12Aatif SyedAatif SyedRUSTrust-1.93.1Error
Feb 23, 2026 16:58Aatif SyedAatif SyedRUSTrust-1.93.1Success105,000+14.29 RP
Feb 23, 2026 16:52Aatif SyedAatif SyedRUSTrust-1.93.1Success123,541+15.67 RP
Feb 23, 2026 16:46Aatif SyedAatif SyedRUSTrust-1.93.1Success191,321
Feb 23, 2026 16:44Aatif SyedAatif SyedRUSTrust-1.93.1Success154,909
Feb 23, 2026 16:41mhorsley30896mhorsley30896RUSTrust-1.93.1Success57,140
Feb 23, 2026 16:31mhorsley30896mhorsley30896RUSTrust-1.93.1Success58,859
Feb 23, 2026 16:28Aatif SyedAatif SyedRUSTrust-1.93.1Success157,497
Feb 23, 2026 16:27Aatif SyedAatif SyedRUSTrust-1.93.1Success435,997
Feb 23, 2026 16:23mhorsley30896mhorsley30896RUSTrust-1.93.1Success51,207+84.42 RP
Feb 23, 2026 16:22Aatif SyedAatif SyedRUSTrust-1.93.1Success153,190+65.28 RP
Feb 23, 2026 16:18mhorsley30896mhorsley30896RUSTrust-1.93.1Success91,224
Feb 23, 2026 16:10mhorsley30896mhorsley30896RUSTrust-1.93.1Success90,200+89.57 RP
Feb 23, 2026 16:08Aatif SyedAatif SyedRUSTrust-1.93.1Error
Feb 23, 2026 15:56mhorsley30896mhorsley30896RUSTrust-1.93.1Success469,574+21.30 RP
Feb 23, 2026 15:39Aatif SyedAatif SyedRUSTrust-1.93.1Error
Feb 23, 2026 15:37Aatif SyedAatif SyedRUSTrust-1.93.1Error
Feb 23, 2026 15:16Jem BishopJem BishopRUSTrust-1.93.1Success41,145+16.24 RP
Feb 23, 2026 15:02Jem BishopJem BishopRUSTrust-1.93.1Success44,091+11.17 RP
Feb 23, 2026 12:23Jem BishopJem BishopRUSTrust-1.93.1Success46,376+25.25 RP
Feb 23, 2026 11:49Jem BishopJem BishopRUSTrust-1.93.1Success52,528+123.18 RP
Feb 23, 2026 11:22Jem BishopJem BishopRUSTrust-1.93.1Success148,826+4.71 RP
Feb 23, 2026 11:18Jem BishopJem BishopRUSTrust-1.93.1Success160,038+62.49 RP
Feb 19, 2026 03:17Josu San MartinJosu San MartinCPPg++13.3.0Success3,734
Feb 19, 2026 03:13Josu San MartinJosu San MartinCPPg++13.3.0Success3,798
Feb 19, 2026 03:03Josu San MartinJosu San MartinCPPg++13.3.0Success3,688+16.08 RP
Feb 19, 2026 02:54Josu San MartinJosu San MartinCPPg++13.3.0Success3,719
Feb 19, 2026 02:36Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:34Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:33Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:32Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:30Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:16Josu San MartinJosu San MartinCPPg++13.3.0Success3,755
Feb 19, 2026 02:15Josu San MartinJosu San MartinCPPg++13.3.0Success3,917
Feb 19, 2026 02:06Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 02:02Josu San MartinJosu San MartinCPPg++13.3.0Success3,755
Feb 19, 2026 01:57Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:54Josu San MartinJosu San MartinCPPg++13.3.0Success3,748
Feb 19, 2026 01:53Josu San MartinJosu San MartinCPPg++13.3.0Success3,710+30.17 RP
Feb 19, 2026 01:49Josu San MartinJosu San MartinCPPg++13.3.0Success3,843
Feb 19, 2026 01:43Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:38Josu San MartinJosu San MartinCPPg++13.3.0Success3,752+19.04 RP
Feb 19, 2026 01:27Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:25Josu San MartinJosu San MartinCPPg++13.3.0Success3,878
Feb 19, 2026 01:22Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:21Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:20Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:19Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:18Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:16Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:15Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 19, 2026 01:10Josu San MartinJosu San MartinCPPg++13.3.0Success4,005