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 28, 2025 21:13Tim ZhamoidinTim ZhamoidinCPPclang++18.1.3Success334,198
Dec 28, 2025 21:10Tim ZhamoidinTim ZhamoidinCPPclang++18.1.3Success404,941
Dec 28, 2025 20:41imotw2imotw2CPPg++13.3.0Success51,645
Dec 28, 2025 20:15imotw2imotw2CPPg++13.3.0Error+51.65 RP
Dec 28, 2025 20:13imotw2imotw2CPPg++13.3.0Success92,972
Dec 28, 2025 19:18yenw0dyenw0dRUSTrust-1.85.1Error
Dec 28, 2025 19:17yenw0dyenw0dRUSTrust-1.85.1Error
Dec 28, 2025 19:07Josu San MartinJosu San MartinGOgo1.25.5Error
Dec 28, 2025 19:06yenw0dyenw0dRUSTrust-1.85.1Error
Dec 28, 2025 19:06Josu San MartinJosu San MartinGOgo1.25.5Error
Dec 28, 2025 19:05yenw0dyenw0dRUSTrust-1.85.1Error+213.21 RP
Dec 28, 2025 19:05Josu San MartinJosu San MartinGOgo1.25.5Error
Dec 28, 2025 19:04Josu San MartinJosu San MartinGOgo1.25.5Error
Dec 28, 2025 19:03Josu San MartinJosu San MartinGOgo1.25.5Error
Dec 28, 2025 18:59Josu San MartinJosu San MartinGOgo1.25.5Success53,547
Dec 28, 2025 18:43Josu San MartinJosu San MartinZIG0.14.0Error
Dec 28, 2025 18:42Josu San MartinJosu San MartinZIG0.14.0Error
Dec 28, 2025 18:41Josu San MartinJosu San MartinZIG0.14.0Error
Dec 28, 2025 18:35Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 28, 2025 18:34Josu San MartinJosu San MartinCPPg++13.3.0Error+34.50 RP
Dec 28, 2025 18:27Josu San MartinJosu San MartinZIG0.14.0Success118,803
Dec 28, 2025 18:27imotw2imotw2CPPg++13.3.0Success67,572+15.84 RP
Dec 28, 2025 18:27imotw2imotw2CPPg++13.3.0Success143,036
Dec 28, 2025 18:25Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 18:24Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 18:24Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 18:23Josu San MartinJosu San MartinRUSTrust-1.92.0Error+189.48 RP
Dec 28, 2025 18:22Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 18:22HattonuriHattonuriCPPclang++18.1.3Error+6.69 RP
Dec 28, 2025 18:20imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 18:18Josu San MartinJosu San MartinCPPg++13.3.0Error+170.27 RP
Dec 28, 2025 18:18Josu San MartinJosu San MartinCPPclang++18.1.3Error+135.45 RP
Dec 28, 2025 18:18imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 18:17Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 28, 2025 18:11imotw2imotw2CPPg++13.3.0Success75,671+21.89 RP
Dec 28, 2025 18:09Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 18:07imotw2imotw2CPPg++13.3.0Success90,695+2.63 RP
Dec 28, 2025 18:06Josu San MartinJosu San MartinCPPg++13.3.0Error+716.87 RP
Dec 28, 2025 18:05imotw2imotw2CPPg++13.3.0Success101,581
Dec 28, 2025 18:04Josu San MartinJosu San MartinCPPg++13.3.0Error+177.41 RP
Dec 28, 2025 18:03Unays Unays RUSTrust-1.87.0Success97,124+4.04 RP
Dec 28, 2025 17:59Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 28, 2025 17:57Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 28, 2025 17:57imotw2imotw2CPPg++13.3.0Error+39.04 RP
Dec 28, 2025 17:52imotw2imotw2CPPg++13.3.0Error+49.53 RP
Dec 28, 2025 17:46imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:46imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:44imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:42imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:40imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:39imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:37HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 17:35Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 17:29imotw2imotw2CPPg++13.3.0Error
Dec 28, 2025 17:25Unays Unays RUSTrust-1.87.0Error+98.92 RP
Dec 28, 2025 17:23Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 17:23Josu San MartinJosu San MartinRUSTrust-1.92.0Error+2.40 RP
Dec 28, 2025 17:13imotw2imotw2CPPg++13.3.0Success524,790+19.06 RP
Dec 28, 2025 16:20Josu San MartinJosu San MartinRUSTrust-1.92.0Error
Dec 28, 2025 16:20Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 28, 2025 15:55yenw0dyenw0dRUSTrust-1.85.1Error+111.64 RP
Dec 28, 2025 15:54yenw0dyenw0dRUSTrust-1.85.1Error+171.61 RP
Dec 28, 2025 15:49yenw0dyenw0dRUSTrust-1.85.1Error+111.09 RP
Dec 28, 2025 15:45HattonuriHattonuriCPPg++13.3.0Error+0.26 RP
Dec 28, 2025 15:25yenw0dyenw0dRUSTrust-1.85.1Error
Dec 28, 2025 08:56RussellStrongRussellStrongRUSTrust-1.92.0Success110,964
Dec 28, 2025 08:55RussellStrongRussellStrongRUSTrust-1.92.0Success111,250
Dec 28, 2025 08:39RussellStrongRussellStrongRUSTrust-1.92.0Success111,867
Dec 28, 2025 07:31yenw0dyenw0dRUSTrust-1.85.1Error+50.51 RP
Dec 28, 2025 05:05HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 05:00HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 04:58HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 04:51HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 03:15HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 02:55HattonuriHattonuriCPPg++13.3.0Error+38.04 RP
Dec 28, 2025 02:49HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 02:30HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 02:29HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 02:28HattonuriHattonuriCPPg++13.3.0Error
Dec 28, 2025 01:58Josu San MartinJosu San MartinZIG0.14.0Error
Dec 28, 2025 01:29yenw0dyenw0dRUSTrust-1.85.1Success64,921+2.82 RP
Dec 27, 2025 22:38Josu San MartinJosu San MartinZIG0.14.0Success3,399,831
Dec 27, 2025 22:29Josu San MartinJosu San MartinZIG0.14.0Error
Dec 27, 2025 22:26Josu San MartinJosu San MartinZIG0.13.0Error
Dec 27, 2025 19:43yenw0dyenw0dRUSTrust-1.85.1Success284,447
Dec 26, 2025 13:39HattonuriHattonuriCPPg++13.3.0Error
Dec 26, 2025 12:18HattonuriHattonuriCPPg++13.3.0Error
Dec 26, 2025 11:36HattonuriHattonuriCPPg++13.3.0Error
Dec 26, 2025 11:28HattonuriHattonuriCPPg++13.3.0Error
Dec 26, 2025 11:11HattonuriHattonuriCPPg++13.3.0Error+1.40 RP
Dec 26, 2025 11:04HattonuriHattonuriCPPg++13.3.0Error
Dec 26, 2025 11:02HattonuriHattonuriCPPg++13.3.0Error+3.35 RP
Dec 25, 2025 14:26HattonuriHattonuriCPPg++13.3.0Error
Dec 25, 2025 14:11HattonuriHattonuriCPPg++13.3.0Error
Dec 25, 2025 13:13HattonuriHattonuriCPPg++13.3.0Error
Dec 25, 2025 11:27HattonuriHattonuriCPPg++13.3.0Error
Dec 25, 2025 05:23Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 25, 2025 05:23Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 25, 2025 05:23Josu San MartinJosu San MartinCPPg++13.3.0Error
Dec 25, 2025 03:51Josu San MartinJosu San MartinCPPg++13.3.0Error