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 4, 2024 11:00Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 11:00Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 10:56Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 10:48Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 15:25Tejas G.Tejas G.CPPg++9.4.0Success741,535
Feb 3, 2024 15:24Tejas G.Tejas G.CPPg++9.4.0Success741,104
Feb 3, 2024 15:21Tejas G.Tejas G.CPPg++9.4.0Success740,901+2.30 RP
Feb 3, 2024 15:18Tejas G.Tejas G.CPPg++9.4.0Success1,226,328
Feb 3, 2024 15:18Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 14:48Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 14:31Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 14:28Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 14:23Tejas G.Tejas G.CPPg++9.4.0Error
Feb 3, 2024 14:16Tejas G.Tejas G.CPPg++9.4.0Error
Jan 24, 2024 20:03unopatientunopatientCPPg++9.4.0Success1,724,771
Jan 24, 2024 06:42unopatientunopatientCPPg++9.4.0Success1,717,311
Jan 24, 2024 06:41unopatientunopatientCPPg++9.4.0Success2,246,615
Jan 23, 2024 16:43AnSaAnSaCPPg++9.4.0Success2,251,474+4.44 RP
Jan 21, 2024 06:58stdspstdspRUSTrust-1.75.0Success122,992
Jan 21, 2024 06:49stdspstdspRUSTrust-1.75.0Success124,532
Jan 21, 2024 06:44stdspstdspRUSTrust-1.75.0Success122,424
Jan 21, 2024 06:37stdspstdspRUSTrust-1.75.0Success122,638
Jan 21, 2024 06:36stdspstdspRUSTrust-1.75.0Success128,208
Jan 21, 2024 06:35stdspstdspRUSTrust-1.75.0Success127,757
Jan 21, 2024 06:25stdspstdspRUSTrust-1.75.0Success126,714
Jan 21, 2024 06:21stdspstdspRUSTrust-1.75.0Success122,360
Jan 21, 2024 06:20stdspstdspRUSTrust-1.75.0Success123,482
Jan 21, 2024 06:19stdspstdspRUSTrust-1.75.0Error
Jan 21, 2024 06:01stdspstdspRUSTrust-1.75.0Success125,165
Jan 21, 2024 05:57stdspstdspRUSTrust-1.75.0Success130,668
Jan 21, 2024 05:45stdspstdspRUSTrust-1.75.0Success126,636
Jan 21, 2024 05:43stdspstdspRUSTrust-1.75.0Error
Jan 21, 2024 05:42stdspstdspRUSTrust-1.75.0Success119,898
Jan 21, 2024 05:40stdspstdspRUSTrust-1.75.0Error
Jan 21, 2024 05:25stdspstdspRUSTrust-1.75.0Success119,336+4.12 RP
Jan 21, 2024 05:22stdspstdspRUSTrust-1.75.0Success125,509+0.32 RP
Jan 21, 2024 05:17stdspstdspRUSTrust-1.75.0Success126,015+0.74 RP
Jan 21, 2024 04:50stdspstdspRUSTrust-1.75.0Success131,845
Jan 21, 2024 04:35stdspstdspRUSTrust-1.75.0Success127,197+14.53 RP
Jan 21, 2024 04:30stdspstdspRUSTrust-1.75.0Success201,794
Jan 21, 2024 04:21stdspstdspRUSTrust-1.75.0Success202,651
Jan 21, 2024 00:27stdspstdspRUSTrust-1.75.0Success156,023+0.60 RP
Jan 21, 2024 00:27stdspstdspRUSTrust-1.75.0Success165,310
Jan 21, 2024 00:25stdspstdspRUSTrust-1.75.0Success166,941
Jan 21, 2024 00:24stdspstdspRUSTrust-1.75.0Success164,844
Jan 21, 2024 00:23stdspstdspRUSTrust-1.75.0Success161,480
Jan 21, 2024 00:22stdspstdspRUSTrust-1.75.0Success168,348
Jan 21, 2024 00:20stdspstdspRUSTrust-1.75.0Success175,003
Jan 21, 2024 00:19stdspstdspRUSTrust-1.75.0Success172,421
Jan 21, 2024 00:17stdspstdspRUSTrust-1.75.0Success175,586
Jan 21, 2024 00:13stdspstdspRUSTrust-1.75.0Error
Jan 21, 2024 00:11stdspstdspRUSTrust-1.75.0Error
Jan 20, 2024 23:48stdspstdspRUSTrust-1.75.0Success158,004
Jan 20, 2024 23:46stdspstdspRUSTrust-1.75.0Success39,821,353
Jan 19, 2024 20:47unopatientunopatientRUSTrust-1.75.0Success517,272
Jan 19, 2024 20:46unopatientunopatientRUSTrust-1.75.0Success523,970
Jan 19, 2024 20:46unopatientunopatientRUSTrust-1.75.0Success531,482
Jan 19, 2024 20:45unopatientunopatientRUSTrust-1.75.0Success529,840
Jan 19, 2024 20:23unopatientunopatientRUSTrust-1.75.0Success510,459+0.21 RP
Jan 19, 2024 10:57Tejas G.Tejas G.CPPg++9.4.0Error
Jan 18, 2024 05:51unopatientunopatientRUSTrust-1.75.0Success526,740
Jan 18, 2024 05:36unopatientunopatientRUSTrust-1.75.0Success515,989+0.01 RP
Jan 18, 2024 02:53unopatientunopatientRUSTrust-1.75.0Success525,310
Jan 18, 2024 01:54unopatientunopatientRUSTrust-1.75.0Success522,340
Jan 18, 2024 01:53unopatientunopatientRUSTrust-1.75.0Error
Jan 18, 2024 01:24unopatientunopatientRUSTrust-1.75.0Success516,265+0.01 RP
Jan 18, 2024 01:21unopatientunopatientRUSTrust-1.75.0Success517,946
Jan 18, 2024 01:20unopatientunopatientRUSTrust-1.75.0Success516,469+19.36 RP
Jan 16, 2024 02:23stdspstdspRUSTrust-1.75.0Success157,502+0.37 RP
Jan 16, 2024 02:18stdspstdspRUSTrust-1.75.0Success160,963
Jan 16, 2024 02:05stdspstdspRUSTrust-1.75.0Success158,976
Jan 16, 2024 02:05stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 02:04stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:56stdspstdspRUSTrust-1.75.0Success221,001
Jan 16, 2024 01:53stdspstdspRUSTrust-1.75.0Success158,417+0.08 RP
Jan 16, 2024 01:50stdspstdspRUSTrust-1.75.0Success159,575
Jan 16, 2024 01:15stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:11stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:07stdspstdspRUSTrust-1.75.0Success789,346
Jan 16, 2024 01:06stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:02stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:59stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:56stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:50stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 23:10stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 22:41stdspstdspRUSTrust-1.75.0Success791,729
Jan 15, 2024 22:38stdspstdspRUSTrust-1.75.0Success1,704,037
Jan 15, 2024 22:38stdspstdspRUSTrust-1.75.0Success1,770,860
Jan 15, 2024 22:32stdspstdspRUSTrust-1.75.0Success789,423
Jan 15, 2024 22:28stdspstdspRUSTrust-1.75.0Success795,171
Jan 15, 2024 22:25stdspstdspRUSTrust-1.75.0Success837,435
Jan 15, 2024 22:24stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 22:06stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 18:39stdspstdspRUSTrust-1.75.0Success163,236
Jan 15, 2024 18:30stdspstdspRUSTrust-1.75.0Success162,551
Jan 15, 2024 16:21stdspstdspRUSTrust-1.75.0Success167,436
Jan 15, 2024 16:21stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 15:10stdspstdspRUSTrust-1.75.0Success158,624+0.27 RP
Jan 15, 2024 14:49stdspstdspRUSTrust-1.75.0Success161,375
Jan 15, 2024 14:49stdspstdspRUSTrust-1.75.0Success161,472