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 5, 2024 00:050xkake0xkakeRUSTrust-1.75.0Error
Feb 5, 2024 00:030xkake0xkakeRUSTrust-1.75.0Success167,082+0.09 RP
Feb 5, 2024 00:030xkake0xkakeRUSTrust-1.75.0Success169,124
Feb 4, 2024 23:560xkake0xkakeRUSTrust-1.70.0Success167,868
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.75.0Success168,988
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.75.0Success168,430
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.74.0Success168,270
Feb 4, 2024 23:530xkake0xkakeRUSTrust-1.74.0Success167,343+0.01 RP
Feb 4, 2024 23:520xkake0xkakeRUSTrust-1.74.0Success167,362+0.05 RP
Feb 4, 2024 23:510xkake0xkakeRUSTrust-1.74.0Success329,479
Feb 4, 2024 23:480xkake0xkakeRUSTrust-1.74.0Success175,557
Feb 4, 2024 23:470xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 23:440xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 23:390xkake0xkakeRUSTrust-1.74.0Success167,499+0.38 RP
Feb 4, 2024 23:370xkake0xkakeRUSTrust-1.74.0Success168,571+9.66 RP
Feb 4, 2024 23:270xkake0xkakeRUSTrust-1.74.0Success201,357+1.01 RP
Feb 4, 2024 16:390xkake0xkakeRUSTrust-1.74.0Success207,112
Feb 4, 2024 16:390xkake0xkakeRUSTrust-1.74.0Success208,246
Feb 4, 2024 16:380xkake0xkakeRUSTrust-1.74.0Success240,837
Feb 4, 2024 16:300xkake0xkakeRUSTrust-1.74.0Success207,065
Feb 4, 2024 16:250xkake0xkakeRUSTrust-1.74.0Success206,490
Feb 4, 2024 16:180xkake0xkakeRUSTrust-1.74.0Success205,527+0.22 RP
Feb 4, 2024 16:180xkake0xkakeRUSTrust-1.74.0Success206,475+0.03 RP
Feb 4, 2024 16:110xkake0xkakeRUSTrust-1.74.0Success206,600+25.84 RP
Feb 4, 2024 16:030xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 15:540xkake0xkakeRUSTrust-1.74.0Success2,395,972
Feb 4, 2024 15:20Tejas G.Tejas G.CPPclang++10.0.0Success1,092,572
Feb 4, 2024 15:16Tejas G.Tejas G.CPPclang++10.0.0Success1,527,325
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Success634,010
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:14Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:13Tejas G.Tejas G.RUSTrust-1.75.0Success478,825+1.03 RP
Feb 4, 2024 15:13Tejas G.Tejas G.RUSTrust-1.75.0Success625,201
Feb 4, 2024 15:13Tejas G.Tejas G.CPPg++9.4.0Success1,506,089
Feb 4, 2024 14:55Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 14:53Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 13:09Tejas G.Tejas G.CPPg++9.4.0Success712,786
Feb 4, 2024 13:09Tejas G.Tejas G.CPPclang++10.0.0Success504,044
Feb 4, 2024 13:08Tejas G.Tejas G.CPPclang++10.0.0Success503,741+0.01 RP
Feb 4, 2024 12:41Tejas G.Tejas G.CPPclang++10.0.0Success503,973+6.28 RP
Feb 4, 2024 12:39Tejas G.Tejas G.CPPg++9.4.0Success738,128
Feb 4, 2024 12:25Tejas G.Tejas G.CPPg++9.4.0Success737,467+0.06 RP
Feb 4, 2024 11:01Tejas G.Tejas G.CPPg++9.4.0Error
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